Axios "Cannot use import statement outside module"
P粉439804514
P粉439804514 2023-10-19 19:34:18
0
1
818

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粉439804514
P粉439804514

reply all(1)
P粉419164700

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template