SyntaxError: Cannot use import statement outside a module
See original GitHub issueWhen you use ECMAScript Modules in Node (v13.6.0 for me) combined with Jest, the following error occurs:
SyntaxError: Cannot use import statement outside a module
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
Here is the test code I am trying to execute.
import { checks } from '../../../../src/utils/index.js'
describe('Array type check', () => {
test('to return false when not an array', () => {
expect(checks.array('string')).toEqual(false)
})
})
I suspect the issue is Jest is not configured to run Node code using “type: module” (ECMAScript Modules).
Issue Analytics
- State:
- Created 4 years ago
- Reactions:63
- Comments:29
Top 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 >Cannot use import statement outside module in JavaScript
The "SyntaxError: Cannot use import statement outside a module" occurs when we use the ES6 Modules syntax in a script that was not...
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 >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 FreeTop 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
Top GitHub Comments
A more complete set of steps is:
npm i --save-dev jest babel-jest @babel/preset-env
module.exports
in yourjest.config.js
babel.config.json
file with the followingAfter this, I was able to get files with imports working just fine.
I resolved the issue by doing the following:
then in your
jest.config.js
file put following: