serverless-bundle icon indicating copy to clipboard operation
serverless-bundle copied to clipboard

Error when importing node-fetch

Open chanan opened this issue 2 years ago • 1 comments

When testing a file that imports "node-fetch", I get the jest error regarding imports (other test files in the same project that don't import node-fetch are fine):

● Test suite failed to run
                                                                                                         
    Jest encountered an unexpected token
...snip...
Details:

    C:\Work\Source\datacloud-listener\node_modules\node-fetch\src\index.js:9
    import http from 'node:http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import fetch, { Headers, Request, Response } from "node-fetch";
        | ^
      2 |
      3 | if (!global.fetch) {
      4 |   global.fetch = fetch;

      at Runtime.createScriptFromCode (node_modules/jest-runner/node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (src/fetch-polyfill.js:1:1)

Any idea what the cause of this or how to work around it?

chanan avatar Apr 15 '22 14:04 chanan

I'm experiencing this issue as well. The code runs fine, but not in tests. It doesn't seem like jest is being run with the same configuration.

I don't know what to do. None of the transform/transformIgnorePatterns inscructions on stackoverlfow seem to do anything.

confusingbits avatar Aug 17 '22 00:08 confusingbits