José Carlos
José Carlos

Reputation: 2932

JEST: Cannot import fetch

I'm newbie with JEST. And I'm trying to make a fetch, but to do this I have to import it. But, I've got this error:

Details:

/home/josecarlos/Workspace/BlueCode/server/node_modules/node-fetch/src/index.js:9
import http from 'node:http';
       ^^^^

SyntaxError: Unexpected identifier

  1 | import config from "dotenv";
> 2 | import fetch from "node-fetch";
    | ^
  3 |
  4 | config.config();
  5 |

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (com/functions.js:2:1)

My node version is: v11.15.0

And this is my dependecies:

  "dependencies": {
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^16.0.1",
"helmet": "^4.6.0",
"morgan": "^1.10.0",
"node-fetch": "^3.1.0"
  },

  "devDependencies": {
    "@babel/cli": "^7.16.0",
    "@babel/core": "^7.16.0",
    "@babel/node": "^7.16.0",
    "@babel/preset-env": "^7.16.0",
    "jest": "^27.3.1",
    "jshint": "^2.13.1",
    "nodemon": "^2.0.14"
  }

Upvotes: 2

Views: 2446

Answers (1)

Neill
Neill

Reputation: 517

There was a new major release of node-fetch with breaking changes. If you downgrade node-fetch to a previous version, for example [email protected], your code should work.

for more information: https://github.com/node-fetch/node-fetch/blob/HEAD/docs/v3-UPGRADE-GUIDE.md

Upvotes: 5

Related Questions