5

I'm trying to run Jest and this error keeps preventing me from running any tests:

Error while loading config - 
You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.

at loadCjsOrMjsDefault (node_modules/@babel/core/lib/config/files/module-types.js:59:13)
          at loadCjsOrMjsDefault.next (<anonymous>)
      at readConfigJS (node_modules/@babel/core/lib/config/files/configuration.js:174:47)
          at readConfigJS.next (<anonymous>)
      at Function.<anonymous> (node_modules/@babel/core/lib/gensync-utils/async.js:16:3)
      at evaluateSync (node_modules/gensync/index.js:251:28)
      at Function.sync (node_modules/gensync/index.js:89:14)
      at sync (node_modules/@babel/core/lib/gensync-utils/async.js:56:25)
      at sync (node_modules/gensync/index.js:182:19)

I'm using nodemon and sucrase to run my server, if that's relevant.

My babel config

module.exports = {
   presets: [
      [
         '@babel/preset-env',
         {
            targets: {
               node: 'current'
            }
         }
      ]
   ]
};

My package.json

{
  "type": "module",
  "scripts": {
    "dev": "nodemon src/server.js",
    "test": "jest"
  }
}

1 Answer 1

11

I think the problem is that your package.json says you are using ES6 modules, but your Babel config is using module.exports which is CommonJS (not ES6 modules).

I renamed my babel.config.js to babel.config.cjs and this fixed the problem. I guess you could also change module.exports to export default but I haven't tried this.

3
  • 7
    Wow, changing it from js to cjs really did work, thanks A LOT! Note: just changing it to export default didn't work. Commented May 15, 2021 at 5:31
  • 1
    I also use "type": "module" in package.json Even though for me neither the .cjs or .mjs extensions worked, finally it got fixed by changing the file to babel.config.json and adding to package.json this config for jest: "jest": { "modulePaths": ["<rootDir>"] } Commented Aug 30, 2023 at 11:20
  • @EvgeniyaManolova Thank you very much, this worked for me, please add this as an answer for me to upvote
    – Omkar
    Commented May 14 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.