axios
axios copied to clipboard
Module '"axios"' has no exported member 'AxiosError'.ts(2305)
Describe the issue
Hi,
I have nextjs typescript app and I am using AxiosError like onError?.(error as AxiosError); and I get Module '"axios"' has no exported member 'AxiosError'.ts(2305) error. I installed types doing npm install axios @types/axios still same issue, also they are imported import axios, { AxiosError } from "axios";. How can I fix this?
Example Code
import axios, { AxiosError } from 'axios';
interface ApiResponse {
id: number;
name: string;
}
const fetchData = async () => {
try {
const response = await axios.get<ApiResponse>('http://localhost:8080/api/data');
console.log('Data:', response.data);
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Axios error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}
};
fetchData();
Expected behavior
AxiosError not to show error in typescript
Module '"axios"' has no exported member 'AxiosError'.ts(2305)
Axios Version
1.7.3
Adapter Version
No response
Browser
Chrome
Browser Version
127
Node.js Version
20.15.1
OS
No response
Additional Library Versions
No response
Additional context/Screenshots
No response
@BarishSarac, try removing the node_modules folder and reinstalling all the dependencies. I'm not facing this issue. Also, I don't think there is a need to install @types/axios. If you're still facing this issue, then try removing the node_modules folder, remove axios from package.json, and then reinstall axios.
Hello @Anonymous961,
I created a new nextjs app to test from scratch. So I used npx create-next-app and then installed axios but I still get the error. I activated typescript, so I tested even with @types/axios installed, still getting error. So fresh nextjs app, having axios installed still giving this error.
@BarishSarac, try removing the
node_modulesfolder and reinstalling all the dependencies. I'm not facing this issue. Also, I don't think there is a need to install@types/axios. If you're still facing this issue, then try removing thenode_modulesfolder, removeaxiosfrompackage.json, and then reinstallaxios.
I had the same issue, this fixed it for me
@BarishSarac try using a venv maybe?
@BarishSarac, try removing the
node_modulesfolder and reinstalling all the dependencies. I'm not facing this issue. Also, I don't think there is a need to install@types/axios. If you're still facing this issue, then try removing thenode_modulesfolder, removeaxiosfrompackage.json, and then reinstallaxios.I had the same issue, this fixed it for me
@BarishSarac try using a venv maybe?
@Jey-Alt, thank you for recommendation
It worked when I upgraded packages, deleted node_modules and installed it back
In my case it was that I installed @types/axios by error. The package is unmantained, so don't use it.
Removing it from package.json and "npm i" did the trick
@manuman94 or uninstall the package: npm uninstall @types/axios
@manuman94 or uninstall the package: npm uninstall @types/axios
Uninstalling @types/axios solved my issue, thanks @cloudfocus
i dont know why there is a @types package for axios. makes little sense as we have the dts where we declare types? my suggestion is to rely only on the types from axios as these will be most up to date
i dont know why there is a
@typespackage for axios. makes little sense as we have the dts where we declare types? my suggestion is to rely only on the types from axios as these will be most up to date
The package is not maintained and was probably created to fix things before Axios fully supported TS.
Spent a whole day in this issue to find out in the end that if we install the @types/axios these errors show up
i will add this to the documentation to note not to use this package. i will also try get this removed
the package is deprecated