简体   繁体   English

打字稿/节点错误 [ERR_MODULE_NOT_FOUND]:找不到模块

[英]Typescript/Node Error [ERR_MODULE_NOT_FOUND]: Cannot find module

Converting Project form CJS to ESM将项目形式CJS转换为ESM

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

This is what the import looks like in app.ts :这是app.ts中导入的样子:
    import './config/datadog';
And this is what it looks like for app.js这就是app.js的样子
  import './config/datadog';

Here is my datadog.ts document这是我的 datadog.ts 文件

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

I have configured my 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编辑

I've posted the code on GitHub我已经在 GitHub 上发布了代码

Your need to use TypeScript v4.7 which is currently the TS-Next Version您需要使用目前是 TS-Next 版本的 TypeScript v4.7


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,
  }
}


To Summarize总结

You must set the tsconfig.json keys module and moduleResolution as they are shown below.您必须设置tsconfig.jsonmodulemoduleResolution ,如下所示。

  1. `moduleResolution: "NodeNext" `moduleResolution:“NodeNext”
  2. module: "NodeNext"

You will need TypeScript v4.7您将需要 TypeScript v4.7


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目录中,作为你当前项目的依赖项。

  1. ~$ sudo npm i -g typescript@next // Global Install

  2. ~$ 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.这听起来很难,但一旦你以前做过,它实际上很容易。


For another helpful source:对于另一个有用的来源:

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.

相关问题 打字稿/节点:错误 [ERR_MODULE_NOT_FOUND]:找不到模块 - Typescript/Node: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 错误 [ERR_MODULE_NOT_FOUND]:找不到模块 - Error [ERR_MODULE_NOT_FOUND]: Cannot find module Node+ExpressRouter -&gt; CommonJS 到 ESM --&gt; 导入路由 --&gt; 错误 [ERR_MODULE_NOT_FOUND]: 找不到模块 - Node+ExpressRouter -> CommonJS to ESM --> Import routes --> Error [ERR_MODULE_NOT_FOUND]: Cannot find module 错误 [ERR_MODULE_NOT_FOUND]:找不到 package '.data' - Error [ERR_MODULE_NOT_FOUND]: Cannot find package '.data' 错误 [ERR_MODULE_NOT_FOUND]:找不到 package 'src' - Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'src' 为什么我收到错误 [ERR_MODULE_NOT_FOUND]:运行服务器时找不到模块 - Why I'm getting Error [ERR_MODULE_NOT_FOUND]: Cannot find module when running the server 错误 [ERR_MODULE_NOT_FOUND]:找不到从 build\\server.js 导入的模块“build\\app” - Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'build\app' imported from build\server.js ERR_MODULE_NOT_FOUND - ERR_MODULE_NOT_FOUND 错误 [ERR_MODULE_NOT_FOUND]:找不到从导入的包“模型” - Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'models' imported from internalBinding('errors').triggerUncaughtException(^ 错误 [ERR_MODULE_NOT_FOUND]: 找不到 package 'postcss' - internalBinding('errors').triggerUncaughtException( ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'postcss'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM