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
git clone https://github.com/dandv/jest-typescript-es-modules.gitcd jest-typescript-es-modulesnpm installnpm 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:
- Created 3 years ago
- Reactions:10
- Comments:45 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I had the same problem. Iāve fixed that by including js files in ts-node:
"transform": { "^.+\\.(ts|tsx|js|jsx)?$": "ts-jest" }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
.coffeefiles out there that userequire/CommonJS, including the CoffeeScript codebase itself. My plan is to treat.coffeeas equivalent to.js, as in, ābehave however a.jsfile 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 usesimport/export, I wouldnāt be surprised if there are some folks out there who have been mixingimportandrequirestatements occasionally, since presumably they currently work.