报错: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 ...
随机推荐
- 解锁4K,Xilinx MPSoC ARM + FPGA高清视频采集与显示方案!
当下,随着数字化多媒体技术以令人惊叹的速度不断演进,高清视频处理成为众多领域关注的焦点.今天为大家分享4K HDMI 高清视频方案,基于Xilinx UltraScale+ MPSoC XCZU7EV ...
- 龙哥收集的Github资源——Python量化不要自己造轮子
打开github后,在搜索框中输入下面的项目作者及项目名称,然后点一下 All Github 搜索 格式 user:xxxxx in:name xxxxxxxxxxxxx 举例 user:Cken ...
- Qt音视频开发1-vlc解码播放
一.前言 最开始接触视频监控这块的时候,用的就是vlc作为解码的内核,主要是因为vlc使用简单方便,直接传入一个句柄即可,简单几行代码就可以实现一个视频流播放,很适合初学者使用,也推荐初学者用qt+v ...
- Selenium 自动化浏览器,解决懒加载的网页获取问题
Selenium 自动化浏览器,解决懒加载的网页获取问题.可以用于爬虫这些 在使用 Selenium WebDriver 进行自动化测试时,可以通过设置日志级别来控制输出的日志信息.在 C# 中,可以 ...
- MIPI/LVDS/PCIE/HDMI 设计规范
参考链接: 1.MIPI/LVDS/PCIE/HDMI 2.接口简介(HDMI .eDP/DP.LVDS.VGA.YPbPr.DVI.MHL.MIPI-DSI.VbyOneHS) 3.干货 | 带 ...
- CDS标准视图:功能位置种类描述 I_FlocCategoryText
视图名称:功能位置种类描述 I_FlocCategoryText 视图类型:基础 视图代码: 点击查看代码 @EndUserText.label: 'Functional Location - Tex ...
- 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具
title: 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具 date: 2025/1/11 updated: 2025/1/11 author: cmdragon excerpt: ...
- SpringCloud Alibaba(一) - Nacos 服务注册与发现,OpenFeign远程调用
1.基础项目过目介绍 1.1 数据库创建 1.2 项目模块分布 1.3 测试http接口调用 1.3.1 http接口调用配置类 //http接口调用配置类 @Configuration public ...
- Linux curl brew命令详解
命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具 ...
- 五. Redis 配置内容(详细配置说明)
五. Redis 配置内容(详细配置说明) @ 目录 五. Redis 配置内容(详细配置说明) 1. Units 单位配置 2. INCLUDES (包含)配置 3. NETWORK (网络)配置 ...