jest: Error: SyntaxError: Cannot use import statement outside a module
I 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)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 145
- Comments: 32
Commits related to this issue
- CI: Migrate .babelrc into babel.config.js https://github.com/facebook/jest/issues/9292#issuecomment-625750534 — committed to paulintrognon/paulintrognon.fr by paulintrognon 4 years ago
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
I am having this issue with @react-native-firebase/app v6
My jest config is in the package.json as follows, could it be a problem?
What am I missing? I am new to unit testing so any help will be very much appreciated!!!
I wrote an article to sum up the solutions as i was unable to find a proper solution for the issue. Hope this will help https://xperimentalhamid.com/how-do-i/fix-cannot-use-import-statement-outside-a-module/
I’m having this problem I’ve never had before. What can I do.
I could resolve the issue with this help: https://stackoverflow.com/questions/55794280/jest-fails-with-unexpected-token-on-import-statement
We enabled tests in a Heroku pipeline for one of our apps, and this error started popping up. I can’t replicate it locally, or in a Docker container (node 8 and LTS tested). I have no idea what’s causing this, and the fact that it only happens in my Heroku environment has me scratching my head even more.
UPDATE This turned out to be pretty specific to our codebase and having
NODE_ENV
set toproduction
in our test environment. Heroku defaults to setting that totest
but we were overriding that causing some problems. I doubt this will help future travelers who have the same problem. Godspeed!and how can i solve this problem in a vue project using @vue/composition
it is my jest.config.js
What worked for me was changing the
modules
option of Babel’s ENV preset fromfalse
toauto
.I am having the same issue, i’m using CRA, don’t know what to do.
@lean’s StackOverflow link helped me solve this issue. Thanks!
Ditto 🤦♂️. I had to move over to a
.babelrc
for integration with parcel (they don’t support babel.config.js). Rather annoying that I am now going to have to have both for the time being…I’m seeing this with crossfilter2 with jest unit tests.
Using jest@24.9.0 from react-scripts@3.3.0 importing crossfilter2@1.5.0.
Update: I resolved my issue with crossfilter by upgrading to crossfilter 1.5.1 which changed it’s CommonJS entry point.
I still get the error. Any help please!
Just a quick reminder for everyone (including my future self in about 6 months – Hi 👋 Yes, I’m talking to you!):
This can also occur when you’re trying to import a file that is importing a dependency that is simply not using the ES6 syntax. Most jest configurations are not (re-)compiling files from the
node_modules
directory, so make sure to import the CJS/UMD/ES6 version of your dependency, and not the ESM version. Sounds obvious and is definitely RTFM, but it took me a while to find this 🤷♂️Have a similar issue
Hi everyone, I’m workin’ with a
webpack.config.js'
andmain.js
files, but I’m still getting errors, it must be thenode
version, I’ve the latestv14.4.0
and I’m still getting these errors.I already did the
type: module
inpackage.json
and it doesn’t work because I get another error in the terminal.I always get these errors in all my projects, both with
.ts
and.js
files, some time ago I didn’t get these errors, and I used to work with ES6imports/exports
, but recently, without knowing why, it doesn’t let me work anymore and I gotta import packages and modules with theCommonJS require()
.Facing same problem. .bablerc
package.json
One of the test case which I am executing aboutus.test.js
On running npm test
Is there anything in my initial configuration, as it was working fine when i was using react native 0.53 version.
If you’re using Typescript, then you should install
ts-jest
and configure it!