Error: SyntaxError: Cannot use import statement outside a module
See original GitHub issueI have this issue when I’m trying to run the tests with this configuration:
jest.config.js
module.exports = {
verbose: true,
preset: 'react-native-web'
}
babel.config.js
module.exports = {
presets: [
'@babel/react',
[
'@babel/preset-env',
{
targets: {
esmodules: true,
},
},
],
],
plugins: [
'@babel/plugin-proposal-function-bind',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-syntax-dynamic-import',
[
'module-resolver',
{
alias: {
'^react-native$': 'react-native-web',
},
},
],
],
env: {
production: {
plugins: [
[
'transform-react-remove-prop-types',
{
mode: 'remove',
ignoreFilenames: ['node_modules'],
},
],
],
},
},
}
Then, the error:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react'
^^^^^^
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)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:145
- Comments:32
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 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 >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 >SyntaxError: Cannot use import statement outside a module
This error is one of the most common issue if you are trying to use ES6 features in your JavaScript project. For eg:...
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
I solved the f***ing thing by migrating the
.babelrc
file tobabel.config.js
! Shocker.In my configurations
ts-jest
+jest-puppeteer
I added the followingtransform
property tojest.config.js
:and this solved the issue