Node --experimental-modules - Error: Cannot find module
Node --experimental-modules - Error: Cannot find module
我在尝试导入本地文件时遇到错误,但在使用 npm 包时没有问题。
server.js
import express from 'express'
import next from 'next'
import apis from './src/server/api'
api.js
export default {
ello: 'bye',
jamie: 'hey'
}
正在启动应用程序
node --experimental-modules --inspect server.js
错误
For help, see: https://nodejs.org/en/docs/inspector
(node:20153) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:59
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module '/var/www/goldendemon.hutber.com/src/server/api' imported from /var/www/goldendemon.hutber.com/server.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:59:13)
at Loader.resolve (internal/modules/esm/loader.js:70:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
at link (internal/modules/esm/module_job.js:42:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
如果其他人有这个问题,我正在回答我自己的问题。
事实证明,在实验模式下您需要定义带有扩展名的完整路径。所以我正在尝试导入 index.js
以为它会知道。
修复它:
import express from 'express'
import next from 'next'
import api from './src/server/api/index.js'
如果您使用 .mjs
扩展名命名模块文件,它也应该有效。此外,还提到了启用 ESM 的其他方法 here.
Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:
Files ending in .mjs.
Files ending in .js, or extensionless files, when the nearest parent package.json file contains a top-level field "type" with a value of "module".
Strings passed in as an argument to --eval or --print, or piped to node via STDIN, with the flag --input-type=module.
在节点 12 上,您有 2 个选项:
在 package.json 上使用 type="module",实验模块并指定具有特殊分辨率的扩展,如下所示:
node --experimental-modules --es-module-specifier-resolution=node src/index
或不使用说明符解析。请记住,您必须在每个地方指定文件的扩展名。
或者像我一样,在您的源代码处理 ES 样式模块导入或一些其他非标准 JavaScript 代码(例如 TypeScript)的节点上使用转译。作为参考,请参阅我编写的这个 bash 脚本,在我的 Node 项目中保存为 .script/run-it.sh
:
#!/bin/bash
script=
out_path==/tmp/$script-out.js
npx esbuild --platform=node --bundle --outfile=$out_path $script
node $out_path
我将它作为 运行 脚本添加到我的 package.json
:
"scripts": {
"test": "sst test",
"start": "sst start",
"build": "sst build",
"deploy": "sst deploy",
"remove": "sst remove",
"run-it": "./.script/run-it.sh"
},
和我的目标脚本(import-test.js
),我想要emit/transpile作为JavaScript代码:
import { default as myImport } from './lib/index.js'
console.log(myImport)
现在我运行它:
$ npm run run-it ./import-test.js
> my-nop@0.1.0 run-it /Users/jmquij0106/git/a-rebalancing-act
> ./.script/run-it.sh "./import-test.js"
[Function: main]
底线是让自己省去痛苦,只要在 Node.js 上处理 ES 模块时就发出符合 CommonJS 的代码,参见 this comment/issue。
我在尝试导入本地文件时遇到错误,但在使用 npm 包时没有问题。
server.js
import express from 'express'
import next from 'next'
import apis from './src/server/api'
api.js
export default {
ello: 'bye',
jamie: 'hey'
}
正在启动应用程序
node --experimental-modules --inspect server.js
错误
For help, see: https://nodejs.org/en/docs/inspector
(node:20153) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:59
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module '/var/www/goldendemon.hutber.com/src/server/api' imported from /var/www/goldendemon.hutber.com/server.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:59:13)
at Loader.resolve (internal/modules/esm/loader.js:70:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
at link (internal/modules/esm/module_job.js:42:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
如果其他人有这个问题,我正在回答我自己的问题。
事实证明,在实验模式下您需要定义带有扩展名的完整路径。所以我正在尝试导入 index.js
以为它会知道。
修复它:
import express from 'express'
import next from 'next'
import api from './src/server/api/index.js'
如果您使用 .mjs
扩展名命名模块文件,它也应该有效。此外,还提到了启用 ESM 的其他方法 here.
Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:
Files ending in .mjs.
Files ending in .js, or extensionless files, when the nearest parent package.json file contains a top-level field "type" with a value of "module".
Strings passed in as an argument to --eval or --print, or piped to node via STDIN, with the flag --input-type=module.
在节点 12 上,您有 2 个选项:
在 package.json 上使用 type="module",实验模块并指定具有特殊分辨率的扩展,如下所示:
node --experimental-modules --es-module-specifier-resolution=node src/index
或不使用说明符解析。请记住,您必须在每个地方指定文件的扩展名。
或者像我一样,在您的源代码处理 ES 样式模块导入或一些其他非标准 JavaScript 代码(例如 TypeScript)的节点上使用转译。作为参考,请参阅我编写的这个 bash 脚本,在我的 Node 项目中保存为 .script/run-it.sh
:
#!/bin/bash
script=
out_path==/tmp/$script-out.js
npx esbuild --platform=node --bundle --outfile=$out_path $script
node $out_path
我将它作为 运行 脚本添加到我的 package.json
:
"scripts": {
"test": "sst test",
"start": "sst start",
"build": "sst build",
"deploy": "sst deploy",
"remove": "sst remove",
"run-it": "./.script/run-it.sh"
},
和我的目标脚本(import-test.js
),我想要emit/transpile作为JavaScript代码:
import { default as myImport } from './lib/index.js'
console.log(myImport)
现在我运行它:
$ npm run run-it ./import-test.js
> my-nop@0.1.0 run-it /Users/jmquij0106/git/a-rebalancing-act
> ./.script/run-it.sh "./import-test.js"
[Function: main]
底线是让自己省去痛苦,只要在 Node.js 上处理 ES 模块时就发出符合 CommonJS 的代码,参见 this comment/issue。