【问题标题】:Can't import node-fetch into Typescript无法将 node-fetch 导入 Typescript
【发布时间】:2021-11-22 09:24:25
【问题描述】:

我正在尝试将node-fetch 导入我的Typescript 项目,但我遇到了这个错误:

[ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES Module : /Users/xxx/xxx/xxx/xxx/xxx/xxx/xxx/node_modules/node-fetch/src/index.js ES 模块的 require() 是不支持。

这是我的设置:

  1. 节点:v16.1.0
  2. 打字稿:4.5.2

node-fetch 包在我的项目中导入如下:import fetch from 'node-fetch';'

这是我的tscongif.json

{
  "compilerOptions": {
    "module": "CommonJS",
    "sModuleInterop": true,
    "target": "ES2020",
    "allowJs": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "outDir": "dist",
    "baseUrl": ".",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "typeRoots": [ 
      "src/@types/",
      "node_modules/@types",
    ],
    "strict": true,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noEmit": true,
    "skipLibCheck": true,
  },
  "include": [
    "src/**/*",
    "config/**/*"
  ]
}

我还尝试将"type": "module" 添加到package.json 并将"module": "ES2020" 设置为tsconfig.json,但出现了一个新错误:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts”

要运行我的代码,我使用nodemon 配置如下:

{
  "watch": ["src"],
  "ext": "ts, js, json",
  "execMap": {
    "ts": "NODE_ENV=development ts-node -T"
  }
}

【问题讨论】:

  • 您在哪里看到这些错误?
  • 在我的控制台中...使用nodemon ./src/server.ts,其中nodemon 配置为:{ "watch": ["src"], "ext": "ts, js, json", "execMap": { "ts": "NODE_ENV=development ts-node -T" } }。有关更易读的版本,请参阅问题
  • @jsejcksn 对不起,我忘了给你加标签-.-
  • 您的问题似乎与存储库中工具的配置有关。您需要提供Minimal, Reproducible Example。在这种情况下,假设想要重现您的问题的人只安装了nodenpm。您需要提供重现问题所需的所有文件和步骤。
  • @jsejcksn 在这里您可以找到一个可重现的示例:github.com/riccardo-nigrelli/node-fetch-problem

标签: node.js typescript


【解决方案1】:

您遇到的问题不是您的应用程序代码中的问题,而是由项目配置错误引起的。 ts-node 中的 ESM 支持目前处于试验阶段。查看这个 GitHub 问题以了解当前情况:

https://github.com/TypeStrong/ts-node/issues/1007

根据您在your comment 中分享的repo link 中的数据,以及上述问题中的信息和文档说明here,以下更改将允许您使用npm @987654326 运行您的应用程序@复制仓库中的脚本。


./package.json:

添加以下条目到对象的顶层(这将告诉 Node 你转译的 *.js 文件是 ES 模块并在该模式下运行它们):

{
  "type": "module"
}

./tsconfig.json:

修改 compilerOptions.module 的值到 "esnext"(这将告诉 TypeScript 以 ESM 格式而不是 CJS 发出模块):

{
  "compilerOptions": {
    "module": "esnext"
  }
}

./nodemon.json:

修改 execMap.ts 的值如下(以这种方式配置 NODE_OPTIONS 环境变量可在 ts-node 中启用本机 ESM 支持):

{
  "execMap": {
    "ts": "NODE_ENV=development NODE_OPTIONS='--loader ts-node/esm' ts-node -T"
  }
}

控制台输出:

$ npm run start:dev

> fetch-problem@1.0.0 start:dev
> NODE_ENV=development nodemon ./src/index.ts

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): src/**/*
[nodemon] watching extensions: ts,js,json
[nodemon] starting `NODE_ENV=development NODE_OPTIONS='--loader ts-node/esm' ts-node -T ./src/index.ts`
(node:3273) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
START
[nodemon] clean exit - waiting for changes before restart

【讨论】:

    【解决方案2】:

    node-fetch v3 是一个 ESM-only 模块 - 你不能用 require() 导入它, 根据文档。

    你试过用这个吗:

    import * as fetch from 'node-fetch';

    【讨论】:

    • 是的,我也试过import * as fetch from 'node-fetch';,但问题仍然存在:(ù
    • 您是否尝试过使用 require 以及您使用的是哪个版本?
    • 什么版本?如果node-fetch 的版本我使用的是3.1.0。使用require() 错误是一样的
    • 试试这个配置{ "compilerOptions": { "module": "commonjs", "target": "es2015", "lib": ["es2017"], "declaration": false, "noImplicitAny": false, "removeComments": true, "inlineSourceMap": true, "moduleResolution": "node" }, "include": ["scripts/**/*.ts", "src/**/*.ts", "node_modules/lodash-es/**/*.js"] }
    • 不行...同样的错误
    猜你喜欢
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多