axios
axios copied to clipboard
🐛 [BUG] - TypeError: axios.defaults is undefined in v 1.1.0
Description
I use CDN with https://unpkg.com/axios/dist/axios.min.js
without specify version in my project, and before new version release, I can work it well.
But after new version release, it will show the error:
TypeError: axios.defaults is undefined
I tried to find some release notes or upgrade guide but I saw someone find them too, so I rollback to old version, and it works now! But I have several pages needs to rollback the version 😢
Is rollback version only way to fix this error?
Code
I used axios with Vue.js, and this is the part in my Vue app:
getProjectSec: function (token) {
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
// ^^^^^ Uncaught (in promise) TypeError: axios.defaults is undefined
this.projects(project => {
axios({
url: `api/Project/${project.value}`,
method: 'get',
}).then(res => {
// Do something
}).catch(err => {
console.log(err.response);
})
})
}
Same here. 1.0.0 didn't have this problem, but #4999 was a show stopper for us.
In 1.1.0, I think this another problem introduced by #5030
% yarn node
Welcome to Node.js v16.17.0.
Type ".help" for more information.
> const axios = require('axios')
undefined
> axios.defaults.baseURL = "https://www.google.com"
Uncaught TypeError: Cannot set properties of undefined (setting 'baseURL')
It looks like the default is no longer the default. As a workaround, you can explicitly use it; note the change in the require
line below:
% yarn node
Welcome to Node.js v16.17.0.
Type ".help" for more information.
> const axios = require('axios').default
undefined
> axios.defaults.baseURL = "https://www.google.com"
'https://www.google.com'
Probably? https://github.com/axios/axios/blob/9c3dce366bd594558e5e474ce9135af22a0d9949/dist/node/axios.cjs#L3760
We downgrade to @1.0.0. and it seems fine.
I think it's this commit: https://github.com/axios/axios/pull/5030/commits/819ad75564f472a9066fa4de3eaf416604daddc5
Issue still persists even with release 1.1.2
axios.defaults.headers.common , facing the same issue: TypeError: Cannot read properties of undefined (reading 'defaults')
Fixed for me in 1.1.2.
Still facing the same issue with the 1.1.2 release.
For those following the CommonJS change suggested in README by adding .default
in require, reverting back to const axios = require('axios');
(removing .default
) works for v1.1.2.
(removing
.default
) works for v1.1.2.
It works, withouth TypeScript typings
It works with axios = require('axios').default;
🎉
This problem persist for me. For some reason, when compiling typescript to javascript, it adds a "default" after axios... and terminal is complaining that axios.default is undefined. Example: In index.ts I have: axios.request("my api") When compiling I see in index.js: axios.default.request("my api") and this fails with error : TypeError: Cannot read property 'request' of undefined
Seriously... I've been fighting with this for 3 hours thinking I was messing up something
Any fix for this until the new version is released?
Not an ideal way to fix this but after compiling, remove ['default'] from where it's on any axios request and the compiled ['default']-free file will run properly.