Home  >  Q&A  >  body text

Axios "Cannot use import statement outside module"

I have a Vue.js application with two files containing:

From "axios" Import axios

These files are located in src/lib within the application and contain the import statement on the first line.

Regardless of what package.json says, running the tests on Github will cause Axios 1.0.0 to be installed, and now any tests involving these files will fail with the above error.

Changing the statement to const axios = require("axios") also fails; node_modules/axios/index.js contains an import statement on line 1 and throws the exception there.

One suggestion I often see for issues like this is to add "type": "module" to package.json (at the same level as src/ ). This will cause all tests to fail and require vue.config.js to be renamed to vue.config.cjs. Doing this confuses me: Error: You appear to be using the native ECMAScript module configuration file, which is only supported when running Babel asynchronously , which I don't understand.

Can anyone suggest what to do here?

P粉439804514P粉439804514336 days ago683

reply all(1)I'll reply

  • P粉419164700

    P粉4191647002023-10-20 15:50:04

    I was able to fix this error by adding a force jest import commonjs axios build

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

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

    reply
    0
  • Cancelreply