![](/img/trans.png)
[英]Typescript/Node: Error [ERR_MODULE_NOT_FOUND]: Cannot find module
[英]Error [ERR_MODULE_NOT_FOUND]: Cannot find module
我正在开发一个节点项目(屏幕截图)。 我在 helpers.js 中有一个 function (网址),我在底部导出为:
module.exports = {
urls: urls,
};
在我的 index.js 中,我尝试使用以下命令导入它:
import { urls } from './helpers';
const myUrls = urls(1,3);
当我运行它时,我得到
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/optionhomes11/nodeprojects/hayes/helpers' imported from /home/optionhomes11/nodeprojects/hayes/index.js Did you mean to import ../helpers.js?
我究竟做错了什么?
你用过
"type":"module"
然后确保你有import
文件必须是.js
扩展名
您应该从'./helpers'
导入,而不是'.helpers'
。
试试这个如下
在您的"/home/optionhomes11/nodeprojects/hayes/index.js"
中搜索
并寻找"/home/optionhomes11/nodeprojects/hayes/helpers"
改变
"/home/optionhomes11/nodeprojects/hayes/helpers"
至
"/home/optionhomes11/nodeprojects/hayes/helpers.js"
你没有做错什么。 文件扩展名的 EcmaScript 模块的当前解析算法和导入具有索引文件的目录的能力需要使用实验标志。 见esm ,底部。
所以让你的工作保持原样,而不是
$ node index.js
你做:
$ node --experimental-specifier-resolution=node index.js
您还可以在 package.json 中创建脚本
像这样:
"scripts": {
"start": "NODE_OPTIONS='--experimental-specifier-resolution=node' node src/index.js
我还建议您安装Path Intellisense
VS 代码扩展。 它在处理嵌套路径时真的很有帮助。
当您使用 ECMAScript 模块时,您必须提供文件扩展名: https://nodejs.org/api/esm.html#esm_mandatory_file_extensions
因此,除了在 package.json 上使用"type": "module"
的其他建议之外,您还需要指定文件扩展名import {urls} from './helpers.js'
。 您还可以使用标志--es-module-specifier-resolution=node
使其将 js 文件解析为模块,就像之前使用require
所做的那样
//helpers.js
export default urls;
//index.js
import urls from './helpers.js';
//package.json(名称下)
"type": "module"
发生这种情况是因为在使用 ES 模块时,我们被强制在 import 语句中指定文件扩展名
import * from "./demo.js" // Works fine
import * from "./demo" // Will throw error as you see
注意:以上两个选项在使用 commonJs 时都有效
//如果你改变package.json试试这个
npm i
此答案不需要在执行时使用运行时标志--es-module-specifier-resolution=node
但是,你必须修改你的 ts 源代码,如果有很多文件,这会很痛苦。 而且,修改后的文件将不再在“commonjs”模式下编译,如果你想 go 回来或使用双重“commonjs”/“module”模式。
修改您的tsconfig.json以确保至少以下设置版本:
compilerOptions:{
"lib": ["es2020"],
"module": "ES2022",
"moduleResolution": "node",
"target": "es2022",
}
适用于 typescript 4.6.3。 (请注意 4.6.1 或更低版本)。 *
修改index.js
import {urls} from "#helpers";
修改package.json
"imports": {
"#helpers": "./helpers.js"
}
前导“#”是强制性的
除了不需要节点运行时执行标志之外,这个答案还满足:
*.*ts
源代码(因此,如果您选择这样做(*note),则可以在 commonjs 下编译它)(*note) 使用 rollup 时,需要内联映射 - 因此在开发期间使用 commonjs 并切换到“模块”进行发布有时可能会有优势。
首先修改package.json ,创建rollup.config.js ,然后使用rollup执行 post tsc动作。
package.json
...
"exports":{
"require":"./index.cjs",
"import":"./index.js"
},
"types": "./index.d.ts",
"type": "module" // you already had this
rollup.config.js
// import resolve from "@rollup/plugin-node-resolve";
import dts from "rollup-plugin-dts";
import commonjs from "@rollup/plugin-commonjs";
import * as path from "path";
import pkg from "./package.json";
export default [
{
input: "index.js",
external:[], // you may quash 'unresolved' warning by adding here
output: [
{ file: pkg.exports.require, format: "cjs" },
{ file: pkg.exports.import, format: "es" },
],
plugins: [
commonjs(),
],
},
{
input: "./index.d.ts",
output: [
{ file: pkg.types, format: "es" },
],
plugins: [dts()],
},
];
调用tsc
然后rollup
:
npx tsc
npx rollup -c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.