必须使用 import 加载 ES 模块: ...\node_modules\got\dist\source\index.js

2024-02-22

还有几十个其他问题的标题基本相同,但没有一个答案似乎是相关的,只会增加混乱。

这是我的tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es2017", "es7", "es6", "dom"],
    "declaration": true,
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

这是我输入的内容:

import md5 from 'js-md5';
import got from 'got';
import { Design } from './Design';
...

这是我的错误:

错误 [ERR_REQUIRE_ESM]:必须使用 import 加载 ES 模块:C:\Users\...\node_modules\got\dist\source\index.js 不支持 ES 模块的 require()。

什么。我是not using require, 我在用着import。我在任何其他模块上都没有遇到此错误,那么为什么 Got 不同(并且为什么没有什么是简单的)?

为什么会发生这种情况以及如何解决它?


As per @jsejcksn 的回答 https://stackoverflow.com/a/71215905/2288578,我尝试改变我的tsconfig.json至以下内容:

{
  "compilerOptions": {
    "target": "es6",
    "module": "esnext",
    "lib": ["es2017", "es7", "es6", "dom"],
    "declaration": true,
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

...并添加了"type": "module" to my package.json:

{
    "dependencies": {
        "body-parser": "^1.19.0",
        "express": "^4.17.1",
        "fs-extra": "^10.0.0",
        "got": "^12.0.1",
        "js-md5": "^0.7.3",
        "moment": "^2.29.1",
        "semver": "^7.3.5",
        "typescript": "^4.4.3",
        "uuid": "^8.3.2",
        "@types/node": "^16.9.2",
        "@types/express": "^4.17.13",
        "@types/fs-extra": "^9.0.13",
        "@types/semver": "^7.3.9",
        "@types/uuid": "^8.3.1",
        "@types/js-md5": "^0.4.3"
    },
    "type": "module"
}

但现在我得到了不同的错误:|

src/Logging.ts:1:20 - 错误 TS2792:找不到模块“时刻”。您的意思是将“moduleResolution”选项设置为“node”,还是将别名添加到“paths”选项?

1 个来自“时刻”的导入时刻;

src/SyncAPI.ts:2:17 - 错误 TS2792:找不到模块“got”。您的意思是将“moduleResolution”选项设置为“node”,还是将别名添加到“paths”选项?

2 从“got”导入;


就我而言,这个错误是由ts-node-dev包裹。

如果您使用相同的包(它通常位于开发依赖项中package.json)您可以查看解决步骤我在并行线程中发布 https://stackoverflow.com/a/74921691/11575732。希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

必须使用 import 加载 ES 模块: ...\node_modules\got\dist\source\index.js 的相关文章

随机推荐