Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0.0] TypeError: Cannot read properties of undefined (reading 'create') #5011

Closed
pro-sumer opened this issue Oct 5, 2022 · 48 comments · Fixed by #5162
Closed

[1.0.0] TypeError: Cannot read properties of undefined (reading 'create') #5011

pro-sumer opened this issue Oct 5, 2022 · 48 comments · Fixed by #5162

Comments

@pro-sumer
Copy link

Describe the bug

const client = axios.create()
                     ^

TypeError: Cannot read properties of undefined (reading 'create')
    at Object.<anonymous> (/Users/rob/Developer/Personal/full-feed/test.js:5:22)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.10.0

To Reproduce

#!/usr/bin/env node

const axios = require('axios').default

const client = axios.create()

Expected behavior

Runs without error.

Environment

  • Axios 1.0.0
  • Node.js 18.10.0
  • macOS Monterey 12.6

Additional context/Screenshots

  • 0.27.2: runs without error
  • 1.0.0: TypeError: Cannot read properties of undefined (reading 'create')

(I could not see any breaking change)

@gdelory
Copy link

gdelory commented Oct 5, 2022

Same issue with get:

/home/gui/sambashare/scripts/axios-test.js:4
  const res = await axios.get(`<url>`, {
                          ^

TypeError: Cannot read properties of undefined (reading 'get')

with:

const axios = require('axios').default
const main = async () => {
  await axios.get('<url>')
}
main()

That is a major breaking change

@furdzik
Copy link

furdzik commented Oct 5, 2022

I am getting similar error

Uncaught TypeError: axios.create is not a function
    at eval (api.js:5:1)

My code is - instance:

const axios = require('axios');
const { gateway } = require('../config/environment');
const { version } = require('../../package.json');

const api = axios.create({
  baseURL: gateway,
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
    'Cache-Control': 'no-cache',
    Pragma: 'no-cache',
    'X-Application-Name': 'app-name',
    'X-Application-Version': version
  }
});

api.interceptors.response.use(
  (response) => response.data,
  (error) => { throw error; }
);

module.exports = { api };

And request, for example:

const { api } = require('../config/api');

const getSomeEndpoint = (data) => (
  api.post('endpoint', data)
);

module.exports = { getSomeEndpoint };

I am not sure where is the problem, I can't find it in my code 😞

@gdelory
Copy link

gdelory commented Oct 5, 2022

That's especially an issue since the document says to use .default here

@Angros23
Copy link

Angros23 commented Oct 5, 2022

I am getting similar error

Uncaught TypeError: axios.create is not a function
    at eval (api.js:5:1)

My code is - instance:

const axios = require('axios');
const { gateway } = require('../config/environment');
const { version } = require('../../package.json');

const api = axios.create({
  baseURL: gateway,
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
    'Cache-Control': 'no-cache',
    Pragma: 'no-cache',
    'X-Application-Name': 'app-name',
    'X-Application-Version': version
  }
});

api.interceptors.response.use(
  (response) => response.data,
  (error) => { throw error; }
);

module.exports = { api };

And request, for example:

const { api } = require('../config/api');

const getSomeEndpoint = (data) => (
  api.post('endpoint', data)
);

module.exports = { getSomeEndpoint };

I am not sure where is the problem, I can't find it in my code 😞

Yes, this form is functional, but you don't have the helpers either the types. When you use default the method create return undefined, this is the reason of the error.

@furdzik
Copy link

furdzik commented Oct 6, 2022

@gdelory, @Angros23

With .default app is working correctly, but during the build it crashes.
I am using gatsby and during the build I do the first requests.
(I have build app without default, It passed, and then added default and reloaded, then was OK, but still when I try to build once again, the app not built) 😞

gatsby-node.js: TypeError: Cannot read properties of undefined (reading 'create')

Before the upgrade to 1.0.0 it was working correctly without default.

@jasonsaayman
Copy link
Member

Will look into this asap

@furdzik
Copy link

furdzik commented Oct 6, 2022

Will look into this asap

@jasonsaayman Thank you, I'm also checking, if it is not a problem with Gatsby. Tell me if there is something I can do to help

@jasonsaayman
Copy link
Member

See #5030 merged should fix this.

@furdzik
Copy link

furdzik commented Oct 7, 2022

@jasonsaayman works like a dream, thank you!

@jasonsaayman
Copy link
Member

Please stay on 1.1.0 for now i need to come up with a solution that works for both commonJS and ESM

@womprat
Copy link

womprat commented Oct 12, 2022

Getting this error with Node.js

Import:
const axios = require('axios').default;

Error:
axios.defaults.withCredentials = true;
TypeError: Cannot read properties of undefined (reading 'defaults')

But really it's just erroring everywhere because 'axios' is not defined:
await axios.post(endpoint, params, config);
TypeError: Cannot read properties of undefined (reading 'post')

I rolled back with "npm install axios@0.27.2" and it works fine.

@IgnatG
Copy link

IgnatG commented Oct 13, 2022

It looks like you don't need .default option anymore.

const axios = require('axios');
const client = axios.create({ ...});

this ^ worked for me

@jasonsaayman
Copy link
Member

@womprat for that the latest version should work if you drop the .default

@bag-man
Copy link

bag-man commented Oct 14, 2022

I've noticed that...

import axios from 'axios';

...will reproduce this error, so you need to use:

const axios = require('axios');

Which is maybe something that should be fixed?

@stevenfukase
Copy link

Removing .default will work fine but will cause TS errors

@orgads
Copy link

orgads commented Oct 18, 2022

With 1.1.3 and TS, this code:

import axios from 'axios';
const axiosClient = axios.create();

is transpiled to:

const axios_1 = require("axios");
const axiosClient = axios_1.default.create();

and it fails.

@jasonsaayman
Copy link
Member

@orgads thanks I will have a look

@taishi55
Copy link

taishi55 commented Oct 21, 2022

Same issue with post

looking for a solution for typescript users. require is still required

Works great if you put the past version.

npm install axios@0.27.2

@emmaroland32
Copy link

Same issue post, spent quite some time on this. Version 1.1.0 works fine though

@ltfschoen
Copy link

ltfschoen commented Oct 25, 2022

To write a program I installed Axios with npm install --save axios, which installed v1.1.3, then in index.js I wrote some code using axios.create, and it seems Visual Studio code autocompleted the import statement for me to incorrectly be const { default: axios } = require("axios");, so when I ran my program I got error [TypeError: Cannot read properties of undefined (reading 'create'). To fix it I used the same version and just changed the line manually to simply const axios = require("axios");

@piyusharorawork
Copy link

I am also able to reproduce this issue via following method

Sample TS Code

import axios from "axios

axios.get("http://some-api")

Now after compiling this code

tsc my-code.ts
node my-code.js

We will get the following error

Cannot read properties of undefined (reading 'get')

Now the only work around is to switch back to version 1.1.0

@bryon-cryptoconsults
Copy link

bryon-cryptoconsults commented Oct 26, 2022

I am also able to reproduce this issue via following method

Sample TS Code

import axios from "axios

axios.get("http://some-api")

Now after compiling this code

tsc my-code.ts
node my-code.js

We will get the following error

Cannot read properties of undefined (reading 'get')

Now the only work around is to switch back to version 1.1.0

Same issue
Tried 1.1.0 and 1.1.3

import axios from axios
axios.create

@JVMartin
Copy link

JVMartin commented Oct 27, 2022

Same issue here, axios.create results in TypeError: Cannot read properties of undefined (reading 'create')

Had to switch back to 1.1.0 - huge breaking change

@joao-moonward
Copy link

following

@SirIle
Copy link

SirIle commented Nov 7, 2022

I think this is related to (and should be fixed by) #5174. The fix should be released soon.

@ChaseMorgan2001
Copy link

ChaseMorgan2001 commented Nov 8, 2022

I'm getting this error, not sure if it is related to this issue or not.

import axios from 'axios'
axios({ url, baseURL, headers, method, data, params })
    .then(res => success(res))
    .catch(res => failure(res))
...

TypeError: Cannot read properties of undefined (reading 'toJSON')
    at index.ts:848:1
    at Array.forEach (<anonymous>)
    at Store.get (index.ts:847:1)
    at StateMethodsImpl.getUntracked (index.ts:1135:1)
    at StateMethodsImpl.get (index.ts:1171:1)
    at Object.get (index.ts:1421:1)
    at JSON.stringify (<anonymous>)
    at Object.onSet (Persistence.ts:20:1)
    at index.ts:1037:1
    at index.ts:978:1

@ianriosbaf

@ibbatta
Copy link

ibbatta commented Nov 8, 2022

+1

Following the topic to know when it will be fixed

@csadai
Copy link

csadai commented Nov 11, 2022

+1 same here with 1.1.3 TS->JS

@jasonsaayman
Copy link
Member

Please try the latest pre-release and let me know npm i axios@1.2.0-alpha.1

@Alia5
Copy link

Alia5 commented Nov 12, 2022

Please try the latest pre-release and let me know npm i axios@1.2.0-alpha.1

Seems to work for me

Thank you

@joao-moonward
Copy link

joao-moonward commented Nov 14, 2022

Worked for me too, thanks!

@gcrozariol
Copy link

Please try the latest pre-release and let me know npm i axios@1.2.0-alpha.1

This works!

@kh71
Copy link

kh71 commented Nov 18, 2022

try axios without .default it work version 1.1.3

return await axios
      .get(
        `${baseUrl}?fields=${process.env.Feed_Fields}&access_token=${process.env.fbAPI}&limit=10`,
        {
          headers: {
            Accept: "application/json",
          },
        }
      )
      .then(({ data }) => data);

@bcetienne
Copy link

Please try the latest pre-release and let me know npm i axios@1.2.0-alpha.1

This works, thanks !

@nickali2
Copy link

nickali2 commented Nov 21, 2022

npm i axios@1.2.0-alpha.1

This works for me, too. thanks.

@ch3njust1n
Copy link

Please try the latest pre-release and let me know npm i axios@1.2.0-alpha.1

This worked for me as well on TS 4.6.4

@ctsstc
Copy link

ctsstc commented Dec 16, 2022

If I install axios 1.2.1 that is currently available then I get tons of errors like this:

Argument of type 'import("./node_modules/axios/index").AxiosRequestConfig<any>' is not assignable to parameter of type 'import("./node_modules/@nestjs/axios/node_modules/axios/index").AxiosRequestConfig<any>'.

It seems that the version mismatch breaks it all.

Edit: whoops thought this was on the nestjs github. I guess I'll have to wait for that dependency to update to fix this then.

@Bat-Sheva-Sh
Copy link

having this error- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
at Details.js:18:1
whille trying to axios and catch the data.json this way-
const ExcelExportData = [];
let i = axios.get('https://localhost:44357/User').then(
result => { ExcelExportData = result.data }
)
.catch(error => {
console.error(error.response.data);
})

@orgads
Copy link

orgads commented Feb 9, 2023

having this error

Which axios version?

@abhishek-goel1
Copy link

abhishek-goel1 commented May 22, 2023

Still getting this issue when trying compiling from TS to JS.
axios version 1.4.0
Any update on this?

@thclark
Copy link

thclark commented Jul 18, 2023

Yeah, this isn't fixed on the latest version. Same behaviour as all the above (using Gatsby). ^0.21.1 worked then borked on an update to ^1 and I've tried everything above, so downgraded to ^0.21.

@formatlos
Copy link

same problem here, pinned the version to v.1.2.2 for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.