axios icon indicating copy to clipboard operation
axios copied to clipboard

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

Open pro-sumer opened this issue 2 years ago • 19 comments

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)

pro-sumer avatar Oct 05 '22 12:10 pro-sumer

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

gdelory avatar Oct 05 '22 12:10 gdelory

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 😞

furdzik avatar Oct 05 '22 13:10 furdzik

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

gdelory avatar Oct 05 '22 13:10 gdelory

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.

Angros23 avatar Oct 05 '22 18:10 Angros23

@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.

furdzik avatar Oct 06 '22 06:10 furdzik

Will look into this asap

jasonsaayman avatar Oct 06 '22 06:10 jasonsaayman

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

furdzik avatar Oct 06 '22 06:10 furdzik

See #5030 merged should fix this.

jasonsaayman avatar Oct 06 '22 09:10 jasonsaayman

@jasonsaayman works like a dream, thank you!

furdzik avatar Oct 07 '22 08:10 furdzik

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

jasonsaayman avatar Oct 07 '22 09:10 jasonsaayman

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 [email protected]" and it works fine.

womprat avatar Oct 12 '22 18:10 womprat

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

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

this ^ worked for me

IgnatG avatar Oct 13 '22 12:10 IgnatG

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

jasonsaayman avatar Oct 14 '22 05:10 jasonsaayman

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?

bag-man avatar Oct 14 '22 21:10 bag-man

Removing .default will work fine but will cause TS errors

stevenfukase avatar Oct 17 '22 02:10 stevenfukase

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.

orgads avatar Oct 18 '22 07:10 orgads

@orgads thanks I will have a look

jasonsaayman avatar Oct 18 '22 09:10 jasonsaayman

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 [email protected]

taishi55 avatar Oct 21 '22 06:10 taishi55

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

emmaroland32 avatar Oct 23 '22 00:10 emmaroland32

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

ltfschoen avatar Oct 25 '22 06:10 ltfschoen

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

piyusharorawork avatar Oct 25 '22 16:10 piyusharorawork

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

bryon-cryptoconsults avatar Oct 26 '22 14:10 bryon-cryptoconsults

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

JVMartin avatar Oct 27 '22 22:10 JVMartin

following

joao-moonward avatar Oct 28 '22 03:10 joao-moonward

For Node.js projects without TS, this hack will work fine.

const axios = require('axios').default || require('axios')

The first require() let VSCode show autocomplete when typing, and the second one will be used at runtime.

tonghoangvu avatar Oct 28 '22 08:10 tonghoangvu

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.

I have even tried to check using earlier version(0.27.1, 0.27.2, 1.0.0), looks like issue is still persits.

ShaktiMandal avatar Oct 29 '22 12:10 ShaktiMandal

this is still an issue with version 1.1.3 on TypeScript. the Axios instance is being created but I get the following error TypeError: Cannot read properties of undefined (reading 'isAxiosError')

when catching and testing an error message with axios.isAxiosError(err)

my simplified code is

import axios from 'axios'

try{
  const response = await axios.get(url);
  console.log(response.data);
}
catch (err){
  if (axios.isAxiosError(err)) {
     console.error(err.message)
   } else {
     console.error(err)
   }
}

kob490 avatar Nov 03 '22 11:11 kob490

Can confirm, still an issue with 1.1.3. I thought I might be doing something dumb but ended up here by while searching.

noseratio avatar Nov 03 '22 23:11 noseratio

@kob490 @noseratio I don't believe this fix has been released yet. 1.1.3 is from several weeks ago.

tzappia avatar Nov 03 '22 23:11 tzappia

+1 here Update: changed to 1.1.0 and worked with

import axios, { AxiosInstance } from 'axios'; this.axios = axios.create();

aosatinskyzengo avatar Nov 07 '22 13:11 aosatinskyzengo