Skip to content

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

Closed

Description

@anthea-wu

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

Activity

  1. changed the title [-]🐛 [BUG] - TypeError: axios.defaults is undefined in v 1.0.0 +[/-] [+]🐛 [BUG] - TypeError: axios.defaults is undefined in v 1.1.0[/+] on Oct 7, 2022
  2. zaq42 commented on Oct 7, 2022

    @zaq42

    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')
    
  3. zaq42 commented on Oct 7, 2022

    @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'
    
  4. pie7 commented on Oct 7, 2022

    @pie7

    Probably?

    exports["default"] = axios;

    We downgrade to @1.0.0. and it seems fine.

  5. zaq42 commented on Oct 7, 2022

    @zaq42

    I think it's this commit: 819ad75

    image

  6. demosjarco commented on Oct 7, 2022

    @demosjarco

    Issue still persists even with release 1.1.2

  7. rraja6195 commented on Oct 7, 2022

    @rraja6195

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

  8. zaq42 commented on Oct 8, 2022

    @zaq42

    Fixed for me in 1.1.2.

  9. bakif commented on Oct 8, 2022

    @bakif

    Still facing the same issue with the 1.1.2 release.

  10. gleuch commented on Oct 8, 2022

    @gleuch

    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.

  11. the-csaba commented on Oct 10, 2022

    @the-csaba
    Contributor

    (removing .default) works for v1.1.2.

    It works, withouth TypeScript typings

  12. felix-berlin commented on Oct 11, 2022

    @felix-berlin

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

  13. wayoalamos commented on Oct 14, 2022

    @wayoalamos

    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

  14. fgatti675 commented on Oct 22, 2022

    @fgatti675

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

  15. alexandru96d commented on Oct 25, 2022

    @alexandru96d

    Any fix for this until the new version is released?

  16. donuty commented on Oct 29, 2022

    @donuty

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions