tsconfig 导致 [ERR_REQUIRE_ESM]:必须使用 import 加载 ES 模块

Sh *_*eeb 6 node.js typescript webpack ts-node

我已经知道如何解决该错误[ERR_REQUIRE_ESM]: Must use import to load ES Module

\n

但我注意到这是由设置来引用引起的,paths我不知道确切的原因。node_modulestsconfig.json

\n

tsconfig.json

\n
{\n  "compileOnSave": false,\n  "compilerOptions": {\n       "paths": {\n          "*": ["node_modules/*", "types/*"]                  \xe2\x80\x8b\n       }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

删除 "*": ["node_modules/*", "types/*"]喜欢这个问题,但我想知道为什么?

\n

tsconfig-paths特别是在注册ts-node然后运行时会出现此错误webpack -c webpack.config.ts

\n

tsconfig.json:

\n
{\n  "compileOnSave": false,\n  "compilerOptions": {\n       "paths": {\n          "*": ["node_modules/*", "types/*"]                  \xe2\x80\x8b\n       \xe2\x80\x8b}\n  },\n  "ts-node": {\n     "require": ["tsconfig-paths/register"]\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

完整的错误跟踪:

\n
(node:6087) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /workspace/dibo/projects/ngx-cms/node_modules/colorette/index.js\nrequire() of ES modules is not supported.\nrequire() of /workspace/dibo/projects/ngx-cms/node_modules/colorette/index.js from /workspace/dibo/projects/ngx-cms/node_modules/webpack-cli/lib/utils/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.\nInstead rename /workspace/dibo/projects/ngx-cms/node_modules/colorette/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /workspace/dibo/projects/ngx-cms/node_modules/colorette/package.json.\n\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)\n    at Module.load (internal/modules/cjs/loader.js:937:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:778:12)\n    at Module.require (internal/modules/cjs/loader.js:961:19)\n    at require (/workspace/dibo/projects/ngx-cms/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)\n    at Object.get colors [as colors] (/workspace/dibo/projects/ngx-cms/node_modules/webpack-cli/lib/utils/index.js:3:16)\n    at Object.error (/workspace/dibo/projects/ngx-cms/node_modules/webpack-cli/lib/utils/logger.js:5:58)\n    at runCLI (/workspace/dibo/projects/ngx-cms/node_modules/webpack-cli/lib/bootstrap.js:13:22)\n(Use `node --trace-warnings ...` to show where the warning was created)\n(node:6087) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)\n(node:6087) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\n
Run Code Online (Sandbox Code Playgroud)\n