Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "SyntaxError: Cannot use import statement outside a module" when using node-fetch in Jest with Typescript #1588

Closed
roblframpton opened this issue Jun 21, 2022 · 5 comments

Comments

@roblframpton
Copy link

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 believe this is specifically a problem with node-fetch because it only affects imports from this package. For example, I can do import express from 'express' without any difficulty.

Reproduction

Steps to reproduce the behavior:

  1. Run the following:
npm init
npm install --save-dev jest typescript ts-jest @types/jest node-fetch @types/node-fetch
npx ts-jest config:init
  1. Create the file temp.test.ts:
import fetch from 'node-fetch';

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

Expected behavior

The test calls fetch

Your Environment

software version
node-fetch 3.2.6
node v16.0.0
npm 8.5.5
jest 28.1.1
Operating System Windows 10 x64
@roblframpton
Copy link
Author

I solved this - it was not an issue with node-fetch, apoligies. See here if you need the solution.

@NotMoni NotMoni removed the bug label Jul 13, 2022
@corderop
Copy link

I don't think this has been resolved. It's still happening to me. I don't think the solution is to downgrade the library to the 2.x version.

We will need to upgrade at any moment, which will continue to fail. Is there any solution to get this working with 3.x version?

@jimmywarting
Copy link
Collaborator

An alternative method is switching to using NodeJS v18 \w built in fetch, FormData, and the rest. it's more spec compliant using web streams instead.

@corderop
Copy link

corderop commented Nov 24, 2022

@jimmywarting I didn't know that Node v18 comes with built-in fetch. I will consider switching instead of migrating to the 3.x version.

However, thanks to your fantastic effort with this library. It helped me solve particular problems I was facing with other libraries.

@Plausiblebowhead26
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants