报错: __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的更多相关文章

  1. 解决运行webpack --config webpack.dev.config.js 报错ReferenceError: _dirname is not defined

    控制台报错信息如下 将webpack.dev.config.js中的_dirname 改为 __dirname 注意这里是两个下划线谢谢!感觉好坑

  2. ant-design-vue 报错 ReferenceError: h is not defined

    使用表格,在配置 columns时用到了 customRender,然后就报错了 <script> import FileName from '@/views/admin/document ...

  3. layedit赋值方法(layedit.setContent赋值时报错-ReferenceError: layedit is not defined)

    layedit.setContent赋值时报错 - ReferenceError: layedit is not defined 报错代码: 控制台报错信息 解决方法: 步骤: 1)先赋值: 2)在渲 ...

  4. electron中JS报错:require is not defined的问题解决方法

    Electron已经发布了6.0正式版,升级后发现原来能运行的代码报错提示require is not defined 解决办法: 修改创建BrowserWindow部分的相关代码,设置属性webPr ...

  5. atom markdown报错:AssertionError: html-pdf: Failed to load PhantomJS module.

    今天安装markdown-pdf之后运行的时候报错: AssertionError: html-pdf: Failed to load PhantomJS module. You have to se ...

  6. wepy开发小程序eslint报错error 'getApp' is not defined no-undef

    wepy开发小程序使用getApp().globalData保存全局数据很方便,但是会在控制台看到很多报错:“error 'getApp' is not defined no-undef”,这是esl ...

  7. html页面调用js文件里的函数报错-->方法名 is not defined处理方法

    前几天写了一个时间函数setInterval,然后出现了这个错误:Uncaught ReferenceError: dosave is not defined(…) 找了半天都没发现错在哪,最后找到解 ...

  8. (转)PyCharm报错:“No R interpreter defined: Many R……”——解决办法

    报错截图: 下载安装链接:The Comprehensive R Archive Network

  9. Vue Cli 3 报错:router is not defined

    报错内容: 报错原因: 代码全部放在了路由配置的main.js文件里,router没有定义,使用的时候报undefined 解决方法: 把router.beforeEach放在main.js里面

  10. Angular4.0 项目报错:Unexpected value xxxComponent' declared by the module 'xxxxModule'. Please add a @Pipe...

    最近刚刚开始学习angular 4.0,在网上找了一个小项目教程学习,然而学习的过程有点艰辛,,各种报错,我明明就是按照博主的步骤老老实实走的呀!!话不多说,上bug-  .- Uncaught Er ...

随机推荐

  1. springboot搭建http2服务器和h2c服务器 h2 的http/https 请求服务器

    HTTP2.0特性通俗易懂篇:https://www.cnblogs.com/yingsmirk/p/5248506.html深入了解篇:https://www.jianshu.com/p/67c54 ...

  2. Qt/C++编写视频监控系统83-自定义悬浮条信息

    一.前言 一般视频控件上会给出个悬浮条,这个悬浮条用于显示分辨率或者一些用户期望看到的信息,一般常用的信息除了分辨率以外,还有帧率.封装格式.视频解码器名称.音频解码器名称.实时码率等,由于实际的场景 ...

  3. 一句话,我让 AI 帮我做了个 P 图网站!

    每到过节,不少小伙伴都会给自己的头像 P 个图,加点儿装饰. 比如圣诞节给自己头上 P 个圣诞帽,国庆节 P 个小红旗等等.这是一类比较简单.需求量却很大的 P 图场景,也有很多现成的网站和小程序,能 ...

  4. [软件工具使用记录] windows离线ollama部署本地模型并配置continue实现离线代码补全

    qwen2.5coder发布之后,觉得差不多可以实现离线模型辅助编程了,所以尝试在公司内网部署模型,配合vsocde插件continue实现代码提示.聊天功能. 目前使用qwen2.5coder的32 ...

  5. Solution - 「OurOJ #47407」巧立名目

    \(\mathscr{Description}\)   Private link.   给定一棵含有 \(n\) 个点的带点权树和大小为 \(m\) 的有序点对集合 \(\{(s_i,t_i)\}_{ ...

  6. ASP6.0 (VB) 获取目录下所有图片文件

    <% 'Desc : ASP6.0 (VB) 获取目录下所有图片文件 'by : wgscd 'date : 2024-2-1 dim c_path c_path=Server.MapPath( ...

  7. 理解Java的接口和抽象类

    深入理解Java的接口和抽象类   对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多 不同的地方.很多人在 ...

  8. Golang-编译和工具链12

    http://c.biancheng.net/golang/build/ go build命令(go语言编译命令)完全攻略 Go语言的编译速度非常快.Go 1.9 版本后默认利用Go语言的并发特性进行 ...

  9. biancheng-MongoDB教程

    目录http://c.biancheng.net/mongodb2/ 1NoSQL是什么2MongoDB是什么3Windows安装MongoDB4Linux安装MongoDB5MacOS安装Mongo ...

  10. 在nginx上搭建php服务

    不同的操作系统会有不一样 我这里用的是centos系统 首先下载 yum install php php-fpm php-mysqlnd php-gd php-mbstring 其次我们需要修改配置文 ...