最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find module axios - compatibility issue with typescript - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

This 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:

  1. check if node_modules/axios folder exists.
  2. try rm -rf node_modules and run npm 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
发布评论

评论列表(0)

  1. 暂无评论