reactjs 通过阅读 package.json 升级 React 版本及其依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49828493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upgrading React version and it's dependencies by reading package.json
提问by AlwaysALearner
I have an existing project, which has react@15and all it's dependencies according to that. But now I have to upgrade to react@16along with it's dependencies. Now, the problem is - there are a lot of dependencies and it is very time consuming to identify version of each dependency.
我有一个现有的项目,它具有react@15和所有它的依赖关系。但是现在我必须升级到react@16它的依赖项。现在,问题是 - 有很多依赖项,识别每个依赖项的版本非常耗时。
So, I was wondering if there was a way where I could upgrade the versions of React and it's dependencies mentioned in package.json, without manually modifying the package.jsonfile.
所以,我想知道是否有一种方法可以升级 React 的版本及其在 中提到的依赖项package.json,而无需手动修改package.json文件。
回答by tskjetne
Using npm
使用 npm
Latest version while still respecting the semverin your package.json: npm update <package-name>.
So, if your package.json says "react": "^15.0.0"and you run npm update reactyour package.json will now say "react": "^15.6.2"(the currently latest version of react 15).
最新版本,同时仍然尊重package.json: 中的 semvernpm update <package-name>。所以,如果你的 package.json 说"react": "^15.0.0"并且你运行npm update react你的 package.json 现在会说"react": "^15.6.2"(当前最新版本的 react 15)。
But since you want to go from react 15 to react 16, that won't do.
Latest version regardless of your semver: npm install --save react@latest.
但既然你想从反应 15 到反应 16,那是行不通的。无论您的服务器如何,最新版本:npm install --save react@latest.
If you want a specific version, you run npm install --save react@<version>e.g. npm install --save [email protected].
如果你想要一个特定的版本,你可以运行npm install --save react@<version>例如npm install --save [email protected].
https://docs.npmjs.com/cli/install
https://docs.npmjs.com/cli/install
Using yarn
使用纱线
Latest version while still respecting the semverin your package.json: yarn upgrade react.
最新版本,同时仍然尊重package.json: 中的 semveryarn upgrade react。
Latest version regardless of your semver: yarn upgrade react@latest.
无论您的服务器如何,最新版本:yarn upgrade react@latest.
回答by Joshua Robinson
Yes, you can use Yarn or NPM to edit your package.json.
是的,您可以使用 Yarn 或 NPM 来编辑您的 package.json。
yarn upgrade [package | package@tag | package@version | @scope/]... [--ignore-engines] [--pattern]
yarn upgrade [package | package@tag | package@version | @scope/]... [--ignore-engines] [--pattern]
Something like:
就像是:
yarn upgrade react@^16.0.0
yarn upgrade react@^16.0.0
Then I'd see what warns or errors out and then run yarn upgrade [package]. No need to edit the file manually. Can do everything from the CLI.
然后我会看到什么警告或错误,然后运行yarn upgrade [package]. 无需手动编辑文件。可以从 CLI 执行所有操作。
Or just run yarn upgradeto update all packages to latest, probably a bad idea for a large project. APIs may change, things may break.
或者只是运行yarn upgrade以将所有包更新到最新版本,这对于大型项目来说可能是个坏主意。API 可能会改变,事情可能会中断。
Alternatively, with NPM run npm outdatedto see what packages will be affected. Then
或者,使用 NPM 运行npm outdated以查看哪些包会受到影响。然后
npm update
npm update
https://yarnpkg.com/lang/en/docs/cli/upgrade/
https://yarnpkg.com/lang/en/docs/cli/upgrade/
https://docs.npmjs.com/getting-started/updating-local-packages
https://docs.npmjs.com/getting-started/updating-local-packages
回答by NathanQ
I highly recommend using yarn upgrade-interactiveto update React, or any Node project for that matter. It lists your packages, current version, the latest version, an indication of a Minor, Major, or Patch update compared to what you have, plus a link to the respective project.
我强烈建议使用yarn upgrade-interactive来更新 React 或任何与此相关的 Node 项目。它列出了您的软件包、当前版本、最新版本、与您所拥有的相比的次要、主要或补丁更新的指示,以及指向相应项目的链接。
You run it with yarn upgrade-interactive --latest, check out release notes if you want, go down the list with your arrow keys, choose which packages you want to upgrade by selecting with the space bar, and hit Enterto complete.
您可以使用 运行它yarn upgrade-interactive --latest,如果需要,请查看发行说明,使用箭头键向下移动列表,使用空格键选择要升级的软件包,然后点击Enter完成。
Npm-upgradeis ok but not as slick.
Npm-upgrade还可以,但没有那么流畅。
回答by Sjonchhe
you can update all of the dependencies to their latest version by
npm update
您可以通过以下方式将所有依赖项更新到最新版本
npm update
回答by veer varun singh
If you want to update react use npx update reacton the terminal.
如果你想npx update react在终端上更新 react 使用。
回答by Félix Maroy
Use this command to update react npm install --save [email protected]Don't forget to change 16.12.0 to the latest version or the version you need to setup.
使用这个命令更新 reactnpm install --save [email protected]不要忘记把 16.12.0 改成最新版本或者你需要设置的版本。

