axios icon indicating copy to clipboard operation
axios copied to clipboard

🐛 [BUG] - TypeError: axios.defaults is undefined in v 1.1.0

Open anthea-wu opened this issue 2 years ago • 13 comments

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);
        })
    })
}

anthea-wu avatar Oct 07 '22 01:10 anthea-wu

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')

zaq42 avatar Oct 07 '22 01:10 zaq42

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'

zaq42 avatar Oct 07 '22 01:10 zaq42

Probably? https://github.com/axios/axios/blob/9c3dce366bd594558e5e474ce9135af22a0d9949/dist/node/axios.cjs#L3760

We downgrade to @1.0.0. and it seems fine.

pie7 avatar Oct 07 '22 03:10 pie7

I think it's this commit: https://github.com/axios/axios/pull/5030/commits/819ad75564f472a9066fa4de3eaf416604daddc5

image

zaq42 avatar Oct 07 '22 03:10 zaq42

Issue still persists even with release 1.1.2

demosjarco avatar Oct 07 '22 16:10 demosjarco

axios.defaults.headers.common , facing the same issue: TypeError: Cannot read properties of undefined (reading 'defaults')

rraja6195 avatar Oct 07 '22 23:10 rraja6195

Fixed for me in 1.1.2.

zaq42 avatar Oct 08 '22 07:10 zaq42

Still facing the same issue with the 1.1.2 release.

bakif avatar Oct 08 '22 16:10 bakif

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.

gleuch avatar Oct 08 '22 20:10 gleuch

(removing .default) works for v1.1.2.

It works, withouth TypeScript typings

the-csaba avatar Oct 10 '22 02:10 the-csaba

It works with axios = require('axios').default; 🎉

felix-berlin avatar Oct 11 '22 14:10 felix-berlin

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

wayoalamos avatar Oct 14 '22 06:10 wayoalamos

Seriously... I've been fighting with this for 3 hours thinking I was messing up something

fgatti675 avatar Oct 22 '22 16:10 fgatti675

Any fix for this until the new version is released?

alexandru96d avatar Oct 25 '22 12:10 alexandru96d

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.

donuty avatar Oct 29 '22 03:10 donuty