从节点 12 升级到 14 后无法识别所需功能
Require function not recognized after upgrade from node 12 to 14
我将我的节点版本从 12 升级到 14.5.0(我需要使用 es6 类 和语法,我发现节点 14 支持它)使用这个 link
但是当我在更新后尝试 运行 我的节点 js 应用程序时,它显示了这个:
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js command`
const path = require("path");
^
ReferenceError: require is not defined
at file:///./NodeJsClient/app.js:1:14
at ModuleJob.run (internal/modules/esm/module_job.js:140:23)
at async Loader.import (internal/modules/esm/loader.js:162:24)
[nodemon] app crashed - waiting for file changes before starting...
我需要更新 nodemon 或 npm 吗?我该如何解决?
我的 npm 版本是 6.14.5
nodejs 正在尝试将您的 app.js 文件加载为 ESM 模块文件,但它不是 ESM 模块文件 - 它是 CommonJS 模块文件。 ESM 模块文件中没有 require()
,这就是您收到错误的原因。
由于您的文件是一个 CommonJS 模块,似乎显然有一些指令告诉节点尝试将您的文件作为 ESM 模块加载。最有可能的地方是在你的 package.json 文件中,你应该确保你有:
type: "commonjs"
没有
type: "module"
我将我的节点版本从 12 升级到 14.5.0(我需要使用 es6 类 和语法,我发现节点 14 支持它)使用这个 link 但是当我在更新后尝试 运行 我的节点 js 应用程序时,它显示了这个:
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js command`
const path = require("path");
^
ReferenceError: require is not defined
at file:///./NodeJsClient/app.js:1:14
at ModuleJob.run (internal/modules/esm/module_job.js:140:23)
at async Loader.import (internal/modules/esm/loader.js:162:24)
[nodemon] app crashed - waiting for file changes before starting...
我需要更新 nodemon 或 npm 吗?我该如何解决? 我的 npm 版本是 6.14.5
nodejs 正在尝试将您的 app.js 文件加载为 ESM 模块文件,但它不是 ESM 模块文件 - 它是 CommonJS 模块文件。 ESM 模块文件中没有 require()
,这就是您收到错误的原因。
由于您的文件是一个 CommonJS 模块,似乎显然有一些指令告诉节点尝试将您的文件作为 ESM 模块加载。最有可能的地方是在你的 package.json 文件中,你应该确保你有:
type: "commonjs"
没有
type: "module"