axios: [1.0.0] TypeError: Cannot read properties of undefined (reading 'create')
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)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 18
- Comments: 48 (6 by maintainers)
Commits related to this issue
- Move axios back to ^0.27.2 https://github.com/axios/axios/issues/5011 — committed to aehrc/pathling by johngrimes 2 years ago
- fix: issue calling axios.create undefined The problem is with v1 and in combination with commonjs module such as this SDK. https://github.com/axios/axios/issues/5011 — committed to prokopsimek/buttercms-js by prokopsimek 10 months ago
With 1.1.3 and TS, this code:
is transpiled to:
and it fails.
Please try the latest pre-release and let me know
npm i axios@1.2.0-alpha.1
Same issue with
get
:with:
That is a major breaking change
Still getting this issue when trying compiling from TS to JS. axios version 1.4.0 Any update on this?
It looks like you don’t need
.default
option anymore.this ^ worked for me
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
Please stay on 1.1.0 for now i need to come up with a solution that works for both commonJS and ESM
That’s especially an issue since the document says to use
.default
hereWill look into this asap
I am also able to reproduce this issue via following method
Sample TS Code
Now after compiling this code
We will get the following error
Now the only work around is to switch back to version 1.1.0
See #5030 merged should fix this.
@kob490 @noseratio I don’t believe this fix has been released yet. 1.1.3 is from several weeks ago.
Same issue post, spent quite some time on this. Version 1.1.0 works fine though
I’ve noticed that…
…will reproduce this error, so you need to use:
Which is maybe something that should be fixed?
I am getting similar error
My code is - instance:
And request, for example:
I am not sure where is the problem, I can’t find it in my code 😞
Same issue with
post
looking for a solution for typescript users. require is still required
Works great if you put the past version.
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.
+1 same here with
1.1.3
TS->JSFor Node.js projects without TS, this hack will work fine.
The first
require()
let VSCode show autocomplete when typing, and the second one will be used at runtime.This works!
I think this is related to (and should be fixed by) #5174. The fix should be released soon.
+1 here Update: changed to 1.1.0 and worked with
import axios, { AxiosInstance } from ‘axios’; this.axios = axios.create();
Same issue here,
axios.create
results inTypeError: Cannot read properties of undefined (reading 'create')
Had to switch back to 1.1.0 - huge breaking change
Same issue Tried 1.1.0 and 1.1.3