I installed Axios local to my Typescript project, but generate an error when trying to import it. Error: "Cannot Find module axios".
root folder
npm install --save-dev axios
src/app.ts
import axios from 'axios';
package.json
"devDependencies": {
"axios": "^0.21.1",
It has something to do with my tsconfig.json. If I remove this file, axios is known. If I restore this file, the error es back in VS Code. I know for Lodash package there is a Lodash types (/@types/lodash) to make lodash patible with typescript. Is there an equivelent for axios?
I have this file .\node_modules\axios\index.d.ts that is supposed to help me out with typescript patibility. Somehow it's not working.
I installed Axios local to my Typescript project, but generate an error when trying to import it. Error: "Cannot Find module axios".
root folder
npm install --save-dev axios
src/app.ts
import axios from 'axios';
package.json
"devDependencies": {
"axios": "^0.21.1",
It has something to do with my tsconfig.json. If I remove this file, axios is known. If I restore this file, the error es back in VS Code. I know for Lodash package there is a Lodash types (https://www.npmjs./package/@types/lodash) to make lodash patible with typescript. Is there an equivelent for axios?
I have this file .\node_modules\axios\index.d.ts that is supposed to help me out with typescript patibility. Somehow it's not working.
Share Improve this question edited Feb 2, 2021 at 20:54 Bill asked Feb 2, 2021 at 20:23 BillBill 1,2055 gold badges18 silver badges27 bronze badges 2- is it dev depenedncy? – Alan Omar Commented Feb 2, 2021 at 20:28
- try this npm install --save axios don't install axios with dev dependency – NIKHIL PARIHAR Commented Feb 2, 2021 at 20:33
3 Answers
Reset to default 5This seems to help. It lets TS understand Axios. Even though Axios was supposed to work out of the box.
npm install --save @types/axios
suggestion - use:
npm install --save axios
instead
--save-dev
flag is for those packages that are not part of your app and needed for development purposes such as running tests, transpiling, piling code etc.
However --save-dev
should have been work:
- check if
node_modules/axios
folder exists. - try
rm -rf node_modules
and runnpm install
again
I had the same error in my http.ts
file.
import axios, { Method, AxiosResponse } from 'axios';
const api = axios.create({
baseURL: process.env.HOST_BACKEND,
});
const request = <T>(
method: Method,
url: string,
params: any
): Promise<AxiosResponse<T>> => {
return api.request<T>({
method,
url,
params,
});
};
export default request;
It happens because of typescript usage. I resolved by reinstalling axios npm
package using @types/
like below
npm install --save @types/axios