本文介绍了为什么我编写的 npm 模块在使用 create-react-app 创建项目后安装了这么多包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这个 npm 模块,react-heartbeat,使用 nwb.当我在一个新项目中安装这个模块时,npm i react-heartbeat,在运行 npm init 之后,它只需要不到 2 秒,并且只安装了 1 个包.当我安装这个模块时,再次npm i react-heartbeat,在用create-react-app创建一个项目后,花了将近3分钟,添加了420个包, 删除 218 个包并更新 1257 个包.

I wrote this npm module, react-heartbeat, using nwb. When I install this module in a new project, npm i react-heartbeat, just after running npm init it takes less than 2 seconds and only installs 1 package. When I install this module, again npm i react-heartbeat, after creating a project with create-react-app, it takes nearly 3 minutes, adds 420 packages, removes 218 packages and updates 1257 packages.

我做错了什么?

我的npm模块很简单,只有1个React组件.它在 package.json 中没有依赖项 文件.以下是 nwb 设置项目时创建的对等依赖项和开发依赖项.

My npm module is very simple, just 1 React component. It has no dependencies in the package.json file. The following peer dependencies and dev dependencies that were created when nwb set the project up.

"peerDependencies": {
  "react": "16.x"
},
"devDependencies": {
  "@types/mocha": "^5.2.5",
  "nwb": "^0.23.0",
  "react": "^16.5.2",
  "react-dom": "^16.5.2"
},

我按照 nwb 的 documentation 准备我的模块以供发布(npm run build)和发布我的模块(npm publish).正确的文件夹在我的 package.json 中被列入白名单:

I followed the instructions from nwb's documentation to prepare my module for publishing (npm run build) and to publish my module (npm publish). The proper folders are white-listed in my package.json:

"files": [
  "es",
  "lib",
  "umd"
],

我运行了 npm publish --dry-run 并确认我的项目中只包含以下 7 个文件:

I ran npm publish --dry-run and confirmed that only the following 7 files are included in my project:

package.json
README.md
es/index.js
lib/index.js
umd/react-heartbeat.js
umd/react-heartbeat.min.js
umd/react-heartbeat.min.js.map

我想知道问题是否出在对等或开发依赖项中,但我不知道如何解决这个问题.

I am wondering if the problem is in the peer or dev dependencies, but I am not sure how to fix this.

这是 react-heartbeat 的源代码.可以在 此处 在 npm 上找到它.

Here is the source code for react-heartbeat.It can be found here on npm.

推荐答案

create-react-app 应该安装它的依赖项,但可能失败了.每次运行 npm i 时,它都会安装 package.json 中缺少的依赖项.

create-react-app is supposed to install it's dependencies but maybe it failed. Every time you run npm i it will install missing dependencies from package.json.

确保在安装之前安装依赖项,运行 npm i 并检查任何新内容.

Make sure dependencies are installed before installing yours by running npm i and check nothing new.

这篇关于为什么我编写的 npm 模块在使用 create-react-app 创建项目后安装了这么多包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:06