简体   繁体   English

node-fetch 3.0.0 和 jest 给出了 SyntaxError:不能在模块外使用导入语句

[英]node-fetch 3.0.0 and jest gives SyntaxError: Cannot use import statement outside a module

I'm trying to upgrade my api to use node-fetch 3.0.0.我正在尝试升级我的 api 以使用 node-fetch 3.0.0。 Part of the breaking changes in their documentation is that node-fetch is now a pure ESM module.他们文档中的部分重大变化是 node-fetch 现在是一个纯 ESM 模块。

https://github.com/node-fetch/node-fetch/blob/main/docs/CHANGELOG.md https://github.com/node-fetch/node-fetch/blob/main/docs/CHANGELOG.md

My unit tests have started breaking from this change.我的单元测试已经开始打破这种变化。 I was using jest.requireActual("node-fetch") for the Response object我使用 jest.requireActual("node-fetch") 作为 Response 对象

const { Response } = jest.requireActual("node-fetch");

However, with the new change, I get:但是,随着新的变化,我得到:

"Property 'Response' does not exist on type '{}'." “类型 '{}' 上不存在属性 'Response'。”

I tried changing to an import statement which seems to fix that error:我尝试更改为似乎修复该错误的导入语句:

import { Response } from "node-fetch"

Now, I get the following error when I run my unit tests:现在,运行单元测试时出现以下错误:

Test suite failed to run
                                                                                                                                                                                                                                                                                                                                                                                         
    Jest encountered an unexpected token                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                         
    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                         
    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".                                                                                                                                                                                                                                                                          

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    C:\Users\{mypath}\api\node_modules\node-fetch\src\index.js:9
    import http from 'http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      2 | import { AuthProvider, TokenCache } from "./tokenTypes";
      3 | import { getUserEmail, getUserId, getUserRole } from "../common/authUtil";
    > 4 | import fetch from "node-fetch";

The error seems to be occurring within node-fetch itself.该错误似乎发生在 node-fetch 本身中。

I tried changing the test script in my package.json to match what the jest documentation suggests for ESM modules.我尝试更改 package.json 中的测试脚本以匹配 Jest 文档对 ESM 模块的建议。 https://jestjs.io/docs/ecmascript-modules https://jestjs.io/docs/ecmascript-modules

package.json包.json

"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"

The documentation also suggests changing the transform property in jest.config.js to take an empty object, but my transform object looks like the following for typescript and ts-jest:该文档还建议更改 jest.config.js 中的转换属性以获取一个空对象,但我的转换对象对于 typescript 和 ts-jest 如下所示:

jest.config.js开玩笑的配置文件

transform: { "^.+\\.ts?$": "ts-jest" }

If I change this to an empty object all of my tests break.如果我将其更改为空对象,则所有测试都会中断。 I'm not very familiar with CJS vs ESM, so any help would be appreciated.我对 CJS 与 ESM 不是很熟悉,所以任何帮助将不胜感激。

Fetch 3.0 is designed for using esmodules instead of commonjs. Fetch 3.0 旨在使用 esmodules 而不是 commonjs。 Therefore you have to make sure you import it into a module.因此,您必须确保将其导入到模块中。

For example: to import it to app.js : Add "type":"module" in your package.json and then import it.例如:将其导入 app.js :在 package.json 中添加 "type":"module" 然后将其导入。 Otherwise downgrade fetch to use an older version that supports commonjs like 2.something.否则,请降级 fetch 以使用支持 commonjs 的旧版本,例如 2.something。

For importing it to other files or app.js even, you can also change the extension from .js to mjs that will tell nodejs to treat as a esmodule.为了将其导入其他文件或 app.js,您还可以将扩展名从 .js 更改为 mjs,这将告诉 nodejs 将其视为 esmodule。

I had the same problem!我有同样的问题! adding添加

 transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
    "^.+\\.(js)$": "babel-jest",
  },
  transformIgnorePatterns: [
  ],

to my jest.config.js file solved it.到我的jest.config.js文件解决了它。 and btw these are my versions:顺便说一句,这些是我的版本:

    "jest": "^27.3.1",
    "ts-jest": "^27.0.7",
    "node-fetch": "^3.1.0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest 或 Mocha 与 Vue:SyntaxError:无法在模块外使用 import 语句 - Jest or Mocha with Vue: SyntaxError: Cannot use import statement outside a module 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module Jest - SyntaxError: 无法在模块外使用 import 语句 - Jest - SyntaxError: Cannot use import statement outside a module SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module Jest 无法使用 Typescript 导入(语法错误:无法在模块外使用导入语句) - Jest fails to import with Typescript (SyntaxError: Cannot use import statement outside a module) Jest 无法导入 vue-test-utils 并出现“SyntaxError: Cannot use import statement outside a module”异常 - Jest fails to import vue-test-utils with "SyntaxError: Cannot use import statement outside a module" exception React CKeditor Jest 错误:SyntaxError:无法在模块外使用导入语句 - React CKeditor Jest error: SyntaxError: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM