错误&quot“ syntaxerror:无法在模块之外使用导入语句”。在与Typescript开玩笑中使用node-fetch时

发布于 2025-02-09 22:14:52 字数 1397 浏览 3 评论 0原文

我正在尝试使用Jest在打字稿中编写一个集成测试,该测试使用node-fetch,诸如此类:

import fetch from 'node-fetch';

test('Hello', async () => {
    await fetch("http://www.google.com");
});

但是我遇到了以下错误:

    Jest encountered an unexpected token

    [...]
    
    Details:

    /home/xxx/node_modules/node-fetch/src/index.js:9
    import http from 'node:http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import fetch from 'node-fetch';
        | ^
      2 |
      3 | test('Hello', async () => {
      4 |     await fetch("http://www.google.com");

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (temp.test.ts:1:1)

我尝试了其他问题中给出的一些解决方案工作;我认为我的问题略有不同,因为不是阻止我使用ES6式通常很重要,但是 em 不适合node> node-fetch < /代码>。例如,我可以从“ Express”中进行导入express而无需任何困难。

为了重现此错误,我运行:

npm init
npm install --save-dev jest typescript ts-jest @types/jest node-fetch @types/node-fetch
npx ts-jest config:init
npx jest

它产生以下嘲笑配置:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

I am trying to write an integration test in Typescript with Jest which uses node-fetch, like this:

import fetch from 'node-fetch';

test('Hello', async () => {
    await fetch("http://www.google.com");
});

But I am getting the following error:

    Jest encountered an unexpected token

    [...]
    
    Details:

    /home/xxx/node_modules/node-fetch/src/index.js:9
    import http from 'node:http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import fetch from 'node-fetch';
        | ^
      2 |
      3 | test('Hello', async () => {
      4 |     await fetch("http://www.google.com");

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (temp.test.ts:1:1)

I have tried some of the solutions given in other questions but they don't work; I think that my problem is slightly different because does not prevent me from using ES6-style important in general, but it specifically does not work for node-fetch. For example, I can do import express from 'express' without any difficuly.

To reproduce this error, I ran:

npm init
npm install --save-dev jest typescript ts-jest @types/jest node-fetch @types/node-fetch
npx ts-jest config:init
npx jest

which yields the following Jest config:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2025-02-16 22:14:52

请使用与CommonJ兼容的V2:

npm install node-fetch@2
const fetch = require('node-fetch');

Please use v2 which remains compatible with CommonJS:

npm install node-fetch@2
const fetch = require('node-fetch');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文