将项目从CJS转换为ESM
我正在尝试将我当前的类型记录-节点项目从ESM转换为CJS,但是,我仍然得到下面的错误。
Error [ERR_MODULE_NOT_FOUND]: Cannot find module` 'redacted/dist/config/datadog'
imported from /redacted/dist/app.js
这就是在app.ts**:**中导入的样子
import './config/datadog';这就是app.js看起来的样子
import './config/datadog';
这是我的datadog.ts文档
datadog.ts
import tracer from 'dd-trace';
tracer.init({
logInjection: true,
profiling: true,
appsec: true
});
export default tracer;以下是我通过~/$ 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当使用ts节点运行时,它工作得很好。
node --experimental-specifier-resolution=node --loader ts-node/esm app.ts --project tsconfig.json
我已经将我的tsconfig.json文件配置如下:
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"lib": ["ES2020"],
"moduleResolution": "node",
"esModuleInterop": true,
"rootDir": "./src",
"outDir": "./dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
}
}编辑
我把代码贴在了GitHub上
发布于 2022-05-12 12:20:48
您需要使用TypeScript v4.7,这是当前的TS-下一个版本
一旦升级到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,
}
}
总结
您必须设置tsconfig.json键module和moduleResolution,如下所示。
module: "NodeNext"
您将需要TypeScript v4.7
就我个人而言,我保留了一个全局属性,所以下面我展示了全局安装的命令,但是您真正需要的是将它作为当前项目的依赖项添加到您的node_modules dir中。
~$ sudo npm i -g typescript@next // Global Install~$ npm i -D typescript@next // Add as a Project Dependency
我无法帮助IDE的存在,但是如果您使用VSCode,请使用以下配置,所以您的项目使用ver v4.7。
然后,您需要设置以下配置
"typescript.tsdk": "./node_modules/typescript/lib",
package.json
您还需要为节点启用ESM。。要做到这一点,您需要向package.json添加以下内容
/** @file "package.json" */
{
"type": "module"
}(**.mts**) ...OR您可以对所有文件使用点MTS文件扩展名。两者都有好处,但讨论好处超出了这个答案的范围。
应该是这样的。这听起来很难,但实际上一旦你做过这件事,那就容易了。
关于另一个有用的来源:
用更多的细节来讨论同样的主题。,,我真的建议你去看看。
https://stackoverflow.com/questions/72213760
复制相似问题