简体   繁体   English

未找到模块:导入模块进行反应时无法解析“axios”

[英]Module not found: Can't resolve 'axios' when importing module to react

I'm creating a npm package, that requires axios .我正在创建一个 npm 包,它需要axios

When i import it in my react application i get当我在我的反应应用程序中导入它时,我得到

Module not found: Can't resolve 'axios' in '/Users/******/nodework/consume/node_modules/myModule'找不到模块:无法解析“/Users/******/nodework/consume/node_modules/myModule”中的“axios”

I want the client to be able to install my package module, without having to install another dependency on their end.我希望客户端能够安装我的包模块,而不必在其端安装另一个依赖项。 How would i do that give that im already using peerDepencies and devDependencies ?如果我已经在使用 peerDepencies 和 devDependencies ,我该怎么做?

for example this is my package.json for my module.例如,这是我的模块的 package.json。

{
  "name": "myModule",
  "version": "1.6.4",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "axios": "^0.19.0"
  },
  "peerDependencies": {
    "axios": "^0.19.0"
  }
}

Peer dependencies lets developers who are using your library know they need to install those libraries themselves because the library you wrote is using it.对等依赖项让使用您的库的开发人员知道他们需要自己安装这些库,因为您编写的库正在使用它。 They will get "missing peer dependency" warnings when they install your library and do not have those installed yet.当他们安装您的库但尚未安装这些库时,他们将收到“缺少对等依赖项”警告。

The dev dependencies are typically for build tools like webpack, compilers, etc.开发依赖项通常用于构建工具,如 webpack、编译器等。

Both peer and dev dependencies will not be installed when a developer installs your library.当开发人员安装您的库时,不会同时安装对等和开发依赖项。

The clients will have to do something like npm i your-library axios to install both your library and the peer dependency.客户端必须执行类似npm i your-library axios来安装您的库和对等依赖项。

If you want the developers to solely install your library without having to install extras on their own, like axios in this case, you will have to list is as a regular dependency.如果您希望开发人员单独安装您的库而不必自己安装额外的东西,例如在这种情况下的 axios,您必须将 is 作为常规依赖项列出。

{
  "dependencies": {
    "axios": "^0.19.0"
  }
}

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM