axios icon indicating copy to clipboard operation
axios copied to clipboard

Module '"axios"' has no exported member 'AxiosError'.ts(2305)

Open BarishSarac opened this issue 1 year ago • 10 comments

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 avatar Aug 05 '24 15:08 BarishSarac

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

Anonymous961 avatar Aug 05 '24 20:08 Anonymous961

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.

image

BarishSarac avatar Aug 06 '24 13:08 BarishSarac

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

I had the same issue, this fixed it for me

@BarishSarac try using a venv maybe?

Jey-Alt avatar Aug 11 '24 15:08 Jey-Alt

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

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

BarishSarac avatar Aug 11 '24 18:08 BarishSarac

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 avatar Oct 17 '24 17:10 manuman94

@manuman94 or uninstall the package: npm uninstall @types/axios

cloudfocus avatar Nov 08 '24 14:11 cloudfocus

@manuman94 or uninstall the package: npm uninstall @types/axios

Uninstalling @types/axios solved my issue, thanks @cloudfocus

yogesh-valiya avatar Nov 26 '24 10:11 yogesh-valiya

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

jasonsaayman avatar Nov 27 '24 07:11 jasonsaayman

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

The package is not maintained and was probably created to fix things before Axios fully supported TS.

cloudfocus avatar Nov 27 '24 08:11 cloudfocus

Spent a whole day in this issue to find out in the end that if we install the @types/axios these errors show up

gabrieldocs avatar Jun 04 '25 19:06 gabrieldocs

i will add this to the documentation to note not to use this package. i will also try get this removed

jasonsaayman avatar Aug 24 '25 17:08 jasonsaayman

the package is deprecated

jasonsaayman avatar Aug 24 '25 17:08 jasonsaayman