Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Since 1.1.3 'Cannot use import statement outside a module' #9

Open
yaakovfeldman opened this issue Jan 16, 2022 · 10 comments
Open

Since 1.1.3 'Cannot use import statement outside a module' #9

yaakovfeldman opened this issue Jan 16, 2022 · 10 comments

Comments

@yaakovfeldman
Copy link

Thank you very much for this library. It worked perfectly on version 1.1.2. However, on version 1.1.3 I get the error SyntaxError: Cannot use import statement outside a module when using the firebase package.

My node app uses require but this is somehow getting changed to import.

I made a minimal example at https://github.com/yaakovfeldman/jest-issue

Running npm run test with version 1.1.2 should produce no issues (except for jest complaining that the test suite contains no tests).

Updating to version 1.1.3 produces:

Details:

    C:\Users\yfeld\Code\school databases\jest-issue\node_modules\firebase\app\dist\index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { registerVersion } from '@firebase/app';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | const express = require('express')
    > 2 | const { initializeApp } = require('firebase/app');
        |                           ^
      3 | //const admin = require('firebase-admin');
      4 |
      5 | const app = express()

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

Test Suites: 1 failed, 1 total
@piranna
Copy link
Collaborator

piranna commented Jan 17, 2022

Interesting, https://github.com/yaakovfeldman/jest-issue/blob/939832187ba12d058ffdb347c29643af40786eb7/app.js#L2 is using require(), so according to https://github.com/firebase/firebase-js-sdk/blob/4bd3a888353c26854142b1861f12e0222151af96/packages/firebase/package.json#L33-L39, it should be getting the CommonJS version, but it's getting the default one, although it's a node.js project... Since you are using Jest at https://github.com/yaakovfeldman/jest-issue/blob/939832187ba12d058ffdb347c29643af40786eb7/package.json#L10 without configuring it, I think it's an issue of Jest itself. It used by default a JsDom environment, but seems now it uses of a Node.js one instead. Can you be able to confirm it? Also, you are using Jest 27.3, can you upgrade it to latest 27.4 version?

@exarus
Copy link
Contributor

exarus commented Jan 17, 2022

I'm facing the same issue on latest Jest 27.4.7, gonna downgrade to 1.1.2

SyntaxError: Unexpected token 'export'

@yaakovfeldman
Copy link
Author

Unfortunately, switching jest between node and jsdom did not change anything.

@piranna
Copy link
Collaborator

piranna commented Jan 17, 2022

node { require: './app/dist/index.cjs.js', import: './app/dist/index.mjs' } [] [ 'require', 'default' ]
default ./app/dist/index.esm.js [] [ 'require', 'default' ]

Ok, for some reason, Jest is not providing the node condition, so it can't match. I think it's related to jestjs/jest#11924, once it gets merged this should be automatically fixed.

@exarus
Copy link
Contributor

exarus commented Jan 17, 2022

So this workaround intended to fix jest issue is blocked by another issue of jest. Hilarious 😂. Let's workaround both these

@piranna
Copy link
Collaborator

piranna commented Jan 17, 2022

We could provide the node condition, but will fail for any other environment that's not Node.js.

@exarus
Copy link
Contributor

exarus commented Jan 17, 2022

Maybe let's have the temporary option just for node. Maybe name it with nodeResolutionType or something like that. It's much better than support old version or abandon everyone including node

@piranna
Copy link
Collaborator

piranna commented Jan 17, 2022

I'm open for ideas.

@Nr9
Copy link

Nr9 commented Feb 9, 2022

I have the same error when I upgrade from 1.1.4 to 1.1.5

    /node_modules/@faker-js/faker/dist/esm/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import{eb as a,fb as b,gb as c,hb as d}from"./chunk-KDUTDRUH.js";import"./chunk-7AHD6BL4.js";import"./chunk-JU2V5CFS.js";import"./chunk-3PZV6T5Z.js";d();export{a as Faker,c as default,b as faker};
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      3 | import { any, mock } from 'jest-mock-extended'
      4 | import { z } from 'zod'
    > 5 | import { faker } from '@faker-js/faker'
        | ^
      6 |

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)

@prescottprue
Copy link

I'm also seeing this when upgrading form 1.1.4 to 1.1.5 when using firebase-admin

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

No branches or pull requests

5 participants