报错: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 ...
随机推荐
- JGit的常用功能(提交、回滚,日志查询)
最近项目中要做一个回滚功能,目的是如果这次发布出现了问题,立马回滚到上一次发布的版本,用jgit实现的,具体方法如下: public class GitUtil { private final sta ...
- JVM实战—1.Java代码的运行原理
大纲 1.Java代码到底是如何运行起来的 2.JVM类加载机制的一系列概念 3.JVM中有哪些内存区域及各自的作用 4.JVM的垃圾回收机制的作用 5.问题汇总 1.Java代码到底是如何运行起来的 ...
- Qt/C++音视频开发49-多级连保存和推流设计(同时保存到多个文件/推流到多个平台)
一.前言 近期遇到个用户需要多级联的保存和推流,在ffmpegsave多线程保存类中实现这个功能,越简单越好,就是在推流的同时,能够开启自动转储功能,一边推流的同时一边录像保存到本地视频文件.最初设想 ...
- Qt音视频开发37-识别鼠标按下像素坐标
一.前言 在和视频交互过程中,用户一般需要在显示视频的通道上点击对应的区域,弹出对应的操作按钮,将当前点击的区域或者绘制的多边形区域坐标或者坐标点集合,发送出去,通知其他设备进行处理.比如识别到很多人 ...
- Qt编写可视化大屏电子看板系统24-模块2当月计划
一.前言 延续分层模块化的设计思路,当月计划模块主要包括模具达成率.零件达成率.零件数.每日工序达成数5个子模块,其中模具达成率采用自定义控件环形图展示,比如计划多少达成多少,达成作为进度:零件达成率 ...
- 【量化读书笔记】【打开量化投资的黑箱】Ch.03 阿尔法模型:Qunat如何盈利?
阿尔法模型非常规定义:在交易中关于买卖时机把握和持有头寸选择的技巧. 阿尔法是指扣除市场基准回报之后的投资回报率. 一.两类阿尔法模型:理论驱动型和数据驱动型 1.1理论驱动型阿尔法模型 基于价格数据 ...
- UE4命令行编译工程入门
注:本文同步发布于微信公众号:stringwu的互联网杂谈UE4命令行编译工程入门 0 背景 笔者是个UE4的小白,本文主要记录了一个小白是如何从零UE4的基础,一步步在命令行打出iOS的ipa包的尝 ...
- 深入解析子查询(SUBQUERY):增强 SQL 查询灵活性的强大工具
title: 深入解析子查询(SUBQUERY):增强 SQL 查询灵活性的强大工具 date: 2025/1/12 updated: 2025/1/12 author: cmdragon excer ...
- Docker npm install:npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY 。。reason: unable to get local issuer certificate 解决办法
这个是需要证书导致无法连接,临时解决办法是设置 npm set strict-ssl=false 在 Dockerfile文件里的 RUN npm install 之前添加 RUN npm set ...
- 15个Linux Grep命令使用实例(实用、常用)
Grep命令主要用于从文件中查找指定的字符串.首先建一个demo_file: 复制代码 代码如下: $ cat demo_fileTHIS LINE IS THE 1ST UPPER CASE LIN ...