“Cannot use import statement outside a module” with Axios

I was able to fix this error by forcing jest to import the commonjs axios build by adding

  "jest": {
    "moduleNameMapper": {
      "axios": "axios/dist/node/axios.cjs"
    }
  },

to my package.json. Other solutions using transformIgnorePatterns didn’t work for me.

Leave a Comment