"Cannot use import statement outside a module" with Axios
Full error message
I have a Vue.js application where two files contain:
import axios from "axios"
These files are located in src/lib within the application and include the import statement on their first line.
Running tests on Github causes Axios 1.0.0 to be installed, no matter what the package.json says, and now any test involving these files fails with the above error.
Changing the statement to const axios = require("axios") fails also; node_modules/axios/index.js contains an import statement on line 1 and the exception is thrown there.
A suggestion I've seen quite often for such issues is to add "type": "module" to package.json (which is at the same level as src/). This causes all tests to fail with a demand to rename vue.config.js as vue.config.cjs. Doing that gets me: Error: You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously, which I do not understand.
Can anyone suggest what to do here?Solutionsource: stackoverflow \u2197
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.
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/554c52b65232dc5697daf5901294e71aaf9bdf82617e95c848f7fad13cd3de36hash \u00b7 554c52b65232dc5697daf5901294e71aaf9bdf82617e95c848f7fad13cd3de36