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.

SyntaxError: Cannot use import statement outside a module

See original GitHub issue

Trying to use Ionic with react framework, and when running a basic debug test, I get the following error:

Jest encountered an unexpected token

Details:

C:\Users\<User>\Documents\GitHub\<appname>\node_modules\@codetrix-studio\capacitor-google-auth\dist\esm\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { registerPlugin } from '@capacitor/core';
                                                                                         ^^^^^^
SyntaxError: Cannot use import statement outside a module

  1 | import { IonButton, IonContent, IonHeader,IonInput,IonLoading,IonPage, IonTitle, IonToolbar, IonIcon} from '@ionic/react';
> 2 | import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
    | ^
  3 | import { useEffect, useState } from 'react';
  4 | import { loginUser } from '../firebaseConfig';
  5 | import ExploreContainer from '../components/ExploreContainer';

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/pages/Login.tsx:2:1)

This ONLY occurs when the capactior-google-auth plugin is used, when that’s taken out, the test runs fine, even though there are plenty of other code modules. Does anyone have any idea what’s causing this?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:14

github_iconTop GitHub Comments

1reaction
villesaucommented, Jun 30, 2021

All the node modules. Some times jest holds a cache somewhere inside node modules and certain changes might corrupt it.

I’m not sure if you should keep your jest config inside babel config? use jest.config.js or package.json for that instead? https://jestjs.io/docs/configuration

Also seems that you are using ts-jest as your transformer: "node_modules/variables/.+\\.(j|t)sx?$": "ts-jest" I don’t think that obeys babel config. You need babel-jest for that.

0reactions
MatthewCodescommented, Oct 13, 2022

Running into the same issue for:

    SyntaxError: Cannot use import statement outside a module

       9 | import { Plugins } from '@capacitor/core';
      10 |
    > 11 | import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

jest.config.js

const esModules = ['@codetrix-studio'].join('|');

module.exports = {
    testTimeout: 20000,
    modulePaths: [
    
    "<rootDir>"
  ],
    
    presets:[
      "@babel/preset-env",
      "@babel/preset-react",
      "@babel/preset-typescript"
    ],

    // testPathIgnorePatterns: [
    //   "<rootDir>/node_modules/(?!@codetrix-studio/.*)"
    //   ],
    
    // transformIgnorePatterns: [
    //   "node_modules/(?!@codetrix-studio)"
    // ],

    transform: {
        "node_modules/@codetrix-studio/.+\\.(j|t)sx?$": "babel-jest"
        },

  };

tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "allowJs": true,
    "types": [
      "jest",
      "node"
    ]
  },
  "files": [
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "types": ["jest"],
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "skipLibCheck": true,
    "lib": [
      "ES2020.String",
      "dom",
      "ES2015",
      "es2017"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

Right now everything passes except for Google Auth, and if I uncomment transformIgnorePattern or testPathIgnorePattern everything breaks. I’ve also tried removing rootDir.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >
How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >
SyntaxError: Cannot use import statement outside a module
If you are using node application and want to use the import statement, then you will also get the " Cannot use import...
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