我试图导入一个包,它的package.json
指定了"type": "module"
。 我看到了Can Typescript import CommonJS Modules?,但它的解决方案(--allowJs
)对我不起作用。
我尝试在我的tsconfig.json
中设置各种值,如module
,moduleResolution
和allowJs
。 esModuleInteropt
和allowSyntheticDefaultImports
都设置为true
。
我尝试使用import ... from
,import(...).then()
,import ... = require(...)
以及将我的index.js
和包的index.js
重命名为index.cjs
。 当我在node中运行时,它们都导致相同的错误:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\src\...\node_modules\stringify-object\index.js require() of ES modules is not supported. require() of E:\src\...\node_modules\stringify-object\index.js from E:\src\...\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. Instead rename E:\src\...\node_modules\stringify-object\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\src\...\node_modules\stringify-object\package.json.
我尝试从包的package.json
中删除"type": "module"
,但它依赖其他具有"type": "module"
的包。
上次我尝试弄清楚这个问题时,最终放弃并选择了另一个包。 这真的不应该是一件难事。