npm install 清除 node_modules 中的 react-native [英] npm install clear the react-native in node_modules
问题描述
我在尝试使用 npm install
将 redux
安装到我的 react-native
项目时遇到问题.每当我运行 npm install redux --save
时,node_modules
中的 react-native
目录将被清除.
I face with a problem when try to use npm install
to install redux
to my react-native
project. Any time I do run npm install redux --save
the react-native
directory inside node_modules
will be cleared.
然后我使用 rm -rf node_modules &&npm install
所有 react-native
包都没有安装在 node_modules
中,所以我必须重新创建项目.
Then I use rm -rf node_modules && npm install
the all react-native
package does not install inside node_modules
so I must re-create project.
我也尝试复制 &node_modules
中的 react-redux
和 redux
从另一个项目到我当前的 react-native
项目.但它无法成功,该错误将我引向了 github 上的 issue.我遵循了这个帮助,但它也失败了.
I also try to copy & past react-redux
and redux
in node_modules
from another project to my current react-native
project. But it can't success, the error lead me to the issue on github. I followed this help and it also fail.
其他一些信息:
➜ npm: 5.0.3
➜ npm: 5.0.3
➜ react-native-cli: 2.0.1
➜ react-native-cli: 2.0.1
➜ 反应原生:0.45.0
➜ react-native: 0.45.0
➜ package.json
➜ package.json
{
"name": "MyProjectNAME",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "0.45.0",
"react-redux": "^5.0.5",
"redux": "^3.6.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-jest": "20.0.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-react-native": "1.9.2",
"jest": "20.0.4",
"react-test-renderer": "16.0.0-alpha.12"
},
"jest": {
"preset": "react-native"
}
}
任何建议表示赞赏.谢谢
Any suggest is appreciated. Thank you
推荐答案
很高兴您使用变通方法解决了这个问题,但请允许我解释为什么在您运行 npm install redux 时移除了 react-native 模块 --保存
.
Glad you have solved this issue using a workaround way but please allow me to explain why react-native module was removed when you ran npm install redux --save
.
解决方案:
- 将 package-lock.json 移出项目文件夹(不要删除它,因为您稍后需要检查它)
- 运行
rm -rf node_modules &&npm 安装
- 检查/node_modules 和 react-native 现在应该在那里
- 运行
npm install redux
(npm v5 默认情况下将 --save)安装 redux 而不会删除现有模块
- Move the package-lock.json out of the project folder (Don't delete it yet because you will need to inspect it later on)
- Run
rm -rf node_modules && npm install
- Check the /node_modules and react-native should be there now
- Run
npm install redux
(npm v5 will --save by default) to install redux without having existing modules get removed
什么是 package-lock.json?
npm v5 有很多更改,您可以在此处阅读.其中之一是每当 npm 修改/node_modules 或 package.json 时生成 package-lock.json (lockfile).
There are a bunch of changes for npm v5 which you can read it here. One of them is generating package-lock.json (lockfile) whenever npm modifies /node_modules or package.json.
使用 package-lock.json,任何运行 npm install
(v5) 的人都将获得与您正在开发的完全相同的 node_modules 树.因此,您也必须提交此文件.
With package-lock.json, anyone who runs npm install
(v5) will get the exact same node_modules tree that you were developing on. So, you would have to commit this file too.
为什么 react-native 模块和其他模块在运行 npm install somePackageName
后被删除,即使它们是在 package.json 中定义的?
Why react-native module and others were removed after running npm install somePackageName
even they are defined in the package.json?
删除是因为您现有的节点模块是在 npm v5 之前安装的.如果您使用 npm v5 安装模块(例如 npm install redux
),您会注意到三件事:
The removal happened because of your existing node modules were installed prior to npm v5. If you use npm v5 to install a module (e.g. npm install redux
), you will notice three things:
- package-lock.json 将被生成(或更新如果存在).Redux 及其依赖项都保存在其中.
- redux 的 package.json 与 npm v5 之前安装的节点模块不同(一些额外的字段以 _ 为前缀,例如 _from、_requiredBy、_resolved 等).
- 最后,在 v5 之前安装的任何模块都将被删除,我猜这是因为它的 package.json 中缺少额外的字段 AND 在新生成的 package-lock.json 中不存在.
- package-lock.json will be generated (or updated if exists). Redux and its dependencies are saved into it.
- The redux's package.json is different than node modules that were installed prior to npm v5 (some extra fields prefix with _ e.g. _from, _requiredBy, _resolved etc.).
- Finally, any module installed prior to v5 will be removed which I guess due to the missing extra fields in its package.json AND does not exist in the newly generated package-lock.json.
所以,运行 rm -rf node_modules &&npm install
再次无法解决问题,因为 package-lock.json 文件(记得只有 redux 和它的依赖被保存到文件中吗?你可以检查旧的 package-lock.json)
So, running rm -rf node_modules && npm install
again will not solve the issue because of the package-lock.json file (Remember only redux and its dependencies were saved to the file? You can check the old package-lock.json)
希望这可以帮助其他人.
Hope this might help someone else.
这篇关于npm install 清除 node_modules 中的 react-native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!