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, with TypeScript and ES Modules

See original GitHub issue

šŸ› Bug Report

Filing a separate issue at @SimenB’s suggestion.

After following the steps to get native ESM support, I’m running into the following error in a project that transpiles TypeScript files using @babel/preset-typescript:

/home/dandv/jets/lib.test.ts:1
import { foo } from './lib';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime._execModule (node_modules/jest-runtime/build/index.js:1074:58)

To Reproduce

  1. git clone https://github.com/dandv/jest-typescript-es-modules.git
  2. cd jest-typescript-es-modules
  3. npm install
  4. npm test

Expected behavior

Test passes.

Link to repl or repo (highly encouraged)

https://github.com/dandv/jest-typescript-es-modules

envinfo

System:
    OS: Linux 5.3 Ubuntu 18.04.4 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
  Binaries:
    Node: 13.13.0 - /usr/bin/node
    npm: 6.14.4 - ~/.local/bin/npm
  npmPackages:
    jest: ^25.4.0 => 25.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:45 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
mnajjariancommented, Sep 23, 2020

I had the same problem. I’ve fixed that by including js files in ts-node:

"transform": { "^.+\\.(ts|tsx|js|jsx)?$": "ts-jest" }

4reactions
GeoffreyBoothcommented, Apr 27, 2020

What I’m thinking makes sense is to make that last one behave like js, i.e. infer from the type field.

Hi, I stumbled across this issue and thought I’d say hi. I’m the maintainer of CoffeeScript and I’ve been dealing with this same issue. In my case, there are plenty of legacy .coffee files out there that use require/CommonJS, including the CoffeeScript codebase itself. My plan is to treat .coffee as equivalent to .js, as in, ā€œbehave however a .js file would at this pathā€ā€”so if it’s in a "type": "module" package scope, it’s ESM. I think that’s the safest approach; even if you think all TypeScript you’ll ever encounter uses import/export, I wouldn’t be surprised if there are some folks out there who have been mixing import and require statements occasionally, since presumably they currently work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Cannot use import statement outside a module
The error means Node found an import statement in a file that it does not consider an ECMAScript (ES) module. Adding "type": "module"...
Read more >
Cannot use import statement outside a module [React ...
In this article, we talked about the SyntaxError: Cannot use import statement outside a module error in TypeScript and JavaScript. This errorĀ ...
Read more >
Fix Cannot use import statement outside a module using Node ...
One of the solutions, if you are using typescript, is to export and import your modules in a commonjs way.
Read more >
How to fix SyntaxError: Cannot use import statement outside a ...
The fix for Typescript projects ... To fix the SyntaxError: Cannot use import statement outside a module error you need to change 2...
Read more >
cannot use import statement outside a module - ItsJavaScript
How to fix SyntaxError: cannot use import statement outside a module? Ā· Solution 1 – Add ā€œtypeā€: ā€œmoduleā€ to package.json Ā· Solution 2...
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