报错:ReferenceError: __dirname is not defined in ES module scope
报错: __dirname is not defined in ES module scope
前言
新版 NodeJS 支持通过 ESM 方式导入模块,代码如:
// CommonJS 规范(旧)
const { readFileSync, writeFileSync } = require('fs')
const path = require('path')
// ESModule 规范(新)
import { readFileSync, writeFileSync } from 'fs'
import path from 'path'
// ESModule 规范(最新)
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
在最新 ESModule 规范中,CommonJS 规范的全局方法和全局变量均无法使用:
require() // ESM 规范报错,未定义不能使用
module.exports // 报错,不能使用
exports // 报错,不能使用
__dirname // 报错,不能使用
__filename // 报错,不能使用
报错:ReferenceError: __dirname is not defined in ES module scope
报错原因:现在是
ESM规范,没有全局变量__dirname,在ESM规范中需要自己定义变量才能使用。
// 最新 node 核心包的导入写法
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
// 获取 __filename 的 ESM 写法
const __filename = fileURLToPath(import.meta.url)
// 获取 __dirname 的 ESM 写法
const __dirname = dirname(fileURLToPath(import.meta.url))
报错:ReferenceError: require is not defined in ES module scope, you can use import instead
require在ESM规范中未定义,使用ESM规范的import代替。
// ESModule 规范(新)
import fs from 'fs'
// CommonJS 规范(旧)
const fs = require('fs')
报错:ReferenceError: exports is not defined in ES module scope
exports在ESM规范中未定义,可使用ESM规范的export导出代替。
// ESModule 规范(新)
export const name = 'Megasu'
export const age = 18
// CommonJS 规范(旧)
exports.name = 'Megasu'
exports.age = 18
报错:ReferenceError: module is not defined in ES module scope
module在ESM规范中未定义,可使用ESM规范的export default默认导出代替。
// ESModule 规范(新)
export default {
name: 'Megasu',
age: 18
}
// CommonJS 规范(旧)
module.exports = {
name: 'Megasu',
age: 18
}
报错:ReferenceError: __dirname is not defined in ES module scope的更多相关文章
- 解决运行webpack --config webpack.dev.config.js 报错ReferenceError: _dirname is not defined
控制台报错信息如下 将webpack.dev.config.js中的_dirname 改为 __dirname 注意这里是两个下划线谢谢!感觉好坑
- ant-design-vue 报错 ReferenceError: h is not defined
使用表格,在配置 columns时用到了 customRender,然后就报错了 <script> import FileName from '@/views/admin/document ...
- layedit赋值方法(layedit.setContent赋值时报错-ReferenceError: layedit is not defined)
layedit.setContent赋值时报错 - ReferenceError: layedit is not defined 报错代码: 控制台报错信息 解决方法: 步骤: 1)先赋值: 2)在渲 ...
- electron中JS报错:require is not defined的问题解决方法
Electron已经发布了6.0正式版,升级后发现原来能运行的代码报错提示require is not defined 解决办法: 修改创建BrowserWindow部分的相关代码,设置属性webPr ...
- atom markdown报错:AssertionError: html-pdf: Failed to load PhantomJS module.
今天安装markdown-pdf之后运行的时候报错: AssertionError: html-pdf: Failed to load PhantomJS module. You have to se ...
- wepy开发小程序eslint报错error 'getApp' is not defined no-undef
wepy开发小程序使用getApp().globalData保存全局数据很方便,但是会在控制台看到很多报错:“error 'getApp' is not defined no-undef”,这是esl ...
- html页面调用js文件里的函数报错-->方法名 is not defined处理方法
前几天写了一个时间函数setInterval,然后出现了这个错误:Uncaught ReferenceError: dosave is not defined(…) 找了半天都没发现错在哪,最后找到解 ...
- (转)PyCharm报错:“No R interpreter defined: Many R……”——解决办法
报错截图: 下载安装链接:The Comprehensive R Archive Network
- Vue Cli 3 报错:router is not defined
报错内容: 报错原因: 代码全部放在了路由配置的main.js文件里,router没有定义,使用的时候报undefined 解决方法: 把router.beforeEach放在main.js里面
- Angular4.0 项目报错:Unexpected value xxxComponent' declared by the module 'xxxxModule'. Please add a @Pipe...
最近刚刚开始学习angular 4.0,在网上找了一个小项目教程学习,然而学习的过程有点艰辛,,各种报错,我明明就是按照博主的步骤老老实实走的呀!!话不多说,上bug- .- Uncaught Er ...
随机推荐
- .NET周刊【12月第3期 2024-12-15】
国内文章 重磅推出 Sdcb Chats:一个全新的开源大语言模型前端 https://www.cnblogs.com/sdcb/p/18597030/sdcb-chats-intro Sdcb Ch ...
- Android Studio中使用Java+OpenGL ES创建Android项目
首先是使用android studio生成空白的模板文件,包含一个MainActivity文件, 在onCreate方法中参照如下注释进行修改: package com.example.wang.an ...
- 基于开源IM即时通讯框架MobileIMSDK:RainbowChat v8.1版已发布
关于MobileIMSDK MobileIMSDK 是一套专门为移动端开发的开源IM即时通讯框架,超轻量级.高度提炼,一套API优雅支持UDP .TCP .WebSocket 三种协议,支持iOS.A ...
- NVIDIA-SMI打印信息解析
- UML之发现用例
用例是最简单的UML元素,用例图是最简单的UML图,但它也可能是UML中最有用的元素之一.尽管我们用包将工作分解为工作包.团队任务或单项任务,也就是说包是组织UML中的各种图及元素的工具.但是用例图可 ...
- ClustrixDB-new数据库
ClustrixDB是一种集群式RDBMS,可确保事务处理符合ACID特性,同时可轻松的提供可扩展性和容错能力. ClustrixDB集群由三个或更多节点(联网的同构服务器)组成.ClustrixDB ...
- Kotlin:【定义类】field、计算属性、防态竞争条件
- VuePress 博客搭建系列 33 篇正式完结!
前言 VuePress 博客搭建系列是我写的第 6 个系列文章,前 5 个系列分别是 JavaScript 深入系列,JavaScript 专题系列.underscore 系列.ES6 系列.Type ...
- js脚本实现文本文件格式批量转换
问题: 在Windows环境下,从某些软件中导出的文本格式的数据,选择了默认的ANSI格式.双击打开数据文件后,一切正常,没乱码问题.但是为什么自己的代码里,先按照ANSI格式打开,在转换为UTF8, ...
- 开源数据库生态遇新变数,天翼云TeleDB提供企业数据管理更优解!
近日,知名开源大规模并行处理 (MPP) 数据库Greenplum的源代码在其官方GitHub页面突然消失,引发了用户和开发者的广泛关注, PostgreSQL生态系统或将产生新变数.开源软件在面对商 ...