[英]Typescript/Node Error [ERR_MODULE_NOT_FOUND]: Cannot find module
I am attempting to convert my current TypeScript-Node project from ESM to CJS, however, I keep getting the error below我正在尝试将我当前的 TypeScript-Node 项目从 ESM 转换为 CJS,但是,我不断收到以下错误
Error [ERR_MODULE_NOT_FOUND]: Cannot find module` 'redacted/dist/config/datadog'
imported from /redacted/dist/app.js
app.ts
:这是app.ts
中导入的样子: import './config/datadog';
app.js
这就是app.js
的样子 import './config/datadog';
datadog.ts数据狗.ts
import tracer from 'dd-trace';
tracer.init({
logInjection: true,
profiling: true,
appsec: true
});
export default tracer;
Here is the full printout of the error I am recieving when I execute the app via ~/$ node dist/app.js
.这是我通过~/$ node dist/app.js
执行应用程序时收到的错误的完整打印输出。
> node dist/app.js
node:internal/errors:465
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'redacted/dist/config/datadog' imported from /redacted/dist/app.js
at new NodeError (node:internal/errors:372:5)
at finalizeResolution (node:internal/modules/esm/resolve:405:11)
at moduleResolve (node:internal/modules/esm/resolve:966:10)
at defaultResolve (node:internal/modules/esm/resolve:1176:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:605:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:318:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
at link (node:internal/modules/esm/module_job:78:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v18.0.0
Process finished with exit code 1
It works fine When running using ts-node使用 ts-node 运行时效果很好
node --experimental-specifier-resolution=node --loader ts-node/esm app.ts --project tsconfig.json
tsconfig.json
file like this:我已经像这样配置了我的tsconfig.json
文件: {
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"lib": ["ES2020"],
"moduleResolution": "node",
"esModuleInterop": true,
"rootDir": "./src",
"outDir": "./dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
}
}
Edit编辑
Once you upgrade to typescript@next
which can be done by executing the command ~/$ npm install -D typescript@next
, you will need to make the changes below to your tsconfig.json
file.升级到typescript@next
后,可以通过执行命令~/$ npm install -D typescript@next
来完成,您需要对tsconfig.json
文件进行以下更改。
{
"compilerOptions": {
"lib": [
"ESNext" /* ESNext includes new Level-4 features that were
recently added to the ECMA-262 JS spec */
],
"module": "NodeNext",/* (1 of 2) TS v4.7 settings you need
to change */
"moduleResolution": "NodeNext", /* This is the one that will
specifically solve the error you're
getting. Without the internal changes
made by this, your project will not
resolve modules correctly. */
"esModuleInterop": true, /* This is properly configured. FYI you cannot
change this, it must be set to true. */
/*
THE REST OF THE SETTINGS DO NOT AFFECT THE MODULE TYPE OR HOW TSC
RESOLVES OTHER MODULES */
"target": "ES2021",
"rootDir": "./src",
"outDir": "./dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
}
}
You must set the tsconfig.json
keys module
and moduleResolution
as they are shown below.您必须设置tsconfig.json
键module
和moduleResolution
,如下所示。
module: "NodeNext"
Personally I keep a global property, so below I show the command for the global install, but all you really need is to add it to your node_modules
dir it as a dependency for your current project.就我个人而言,我保留了一个全局属性,所以下面我展示了全局安装的命令,但你真正需要的只是将它添加到你的node_modules
目录中,作为你当前项目的依赖项。
~$ sudo npm i -g typescript@next // Global Install
~$ npm i -D typescript@next // Add as a Project Dependency
I can't help with ever IDE in existance, but if you use VSCode, use the following configuration so your project uses the ver v4.7.我对现有的 IDE 无能为力,但如果您使用 VSCode,请使用以下配置,以便您的项目使用 ver v4.7。
Then you need to set the following configuration然后需要设置如下配置
"typescript.tsdk": "./node_modules/typescript/lib",
package.json
You also need toenable ESM in for Node.您还需要为Node.js 启用 ESM。 . . To do this you need to add the following to your package.json
为此,您需要将以下内容添加到您的package.json
/** @file "package.json" */
{
"type": "module"
}
...OR YOU CAN use the dot MTS ( .mts
) file extension for all of your files. ...或者您可以对所有文件使用点 MTS ( .mts
)文件扩展名。 There are advantages to both, but discussing the advantages is beyond the scope of this answer.两者都有优点,但讨论优点超出了这个答案的范围。
That should be it.应该是这样的。 It sounds hard but its actually easy once you have done it before.这听起来很难,但一旦你以前做过,它实际上很容易。
The answer at this LINK covers this same subject with a bit more detail. 此LINK的答案更详细地涵盖了同一主题。 I really suggest you check it out.我真的建议你检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.