question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Cannot use import statement outside a module for some esm.js dependencies

See original GitHub issue

Version

12.1.0

Please note this didn’t occured in previous versions of preset / jest 27 but we had to update to keep up with angular 14. Keeping old versions along with angular 14 doesn’t work.

Steps to reproduce

On a repository using firebase dependencies, we cannot run tests anymore since update to latest jest-angular-preset and jest 28. At first we didn’t have much configuration

Initial configuration

jest.config.js

  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/src/setupJest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',

Error

/Users/X/dev/ourApp/node_modules/firebase/compat/auth/dist/index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import '@firebase/auth-compat';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1773:14)
      at node_modules/@angular/fire/bundles/angular-fire-compat-auth.umd.js:2:85
      at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire-compat-auth.umd.js:5:2)

fix attempt using babel

jest.config.js

preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/src/setupJest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',
  transform: {
    '^.+\\.(ts|html)$': 'jest-preset-angular',
    '^.+\\.js$': 'babel-jest',
  },

babel.config.js

module.exports = {
  presets: ['@babel/preset-env'],
  env: {
    test: {
      plugins: ["@babel/plugin-transform-runtime"]
    }
  }
};

Error is the same

/Users/X/dev/ourApp/node_modules/firebase/compat/auth/dist/index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import '@firebase/auth-compat';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1773:14)
      at node_modules/@angular/fire/bundles/angular-fire-compat-auth.umd.js:2:85
      at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire-compat-auth.umd.js:5:2)

Expected behavior

We expect preset to manage transforms for all required files and avoid SyntaxError: Cannot use import statement outside a module error on some js files in node_modules.

Actual behavior

SyntaxError: Cannot use import statement outside a module error on some js files in node_modules.

Additional context

No response

Environment

System:
    OS: macOS 12.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 3.2.0 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  npmPackages:
    jest: 28.1.0 => 28.1.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:26 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
blackholegalaxycommented, Jun 14, 2022

Using more detailed resolvers, referencing all sub-packages seemed to do the trick on repro repository. I will try to write some docs about this.

Please note in our case we had to remote transformIgnorePatterns entry then cause whitelisting *.mjs package somehow was causing weird errors.

0reactions
ahnpnlcommented, Aug 20, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Cannot use import statement outside a module
json file, add the top-level "type" field with a value of "module" . This will ensure that all .js and .mjs files are...
Read more >
SyntaxError: Cannot use import statement outside a module ...
When you use ECMAScript Modules in Node (v13.6.0 for me) combined with Jest, the following error occurs: SyntaxError: Cannot use import ...
Read more >
Jest and ESM: Cannot use import statement outside a module
The problem. In a Next.js project, I kept running into an error while using dependencies with ESM modules (.mjs).
Read more >
How to Fix "SyntaxError: Cannot use import statement outside ...
The error "SyntaxError: Cannot use import statement outside a module" is caused by the fact that the file you're trying to import is...
Read more >
syntaxerror cannot use import statement outside a module ...
The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the error,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found