从 npm 迁移
对于大多数用户来说,从 npm 迁移应该是一个相当简单的过程。Yarn 可以使用与 npm 相同的 package.json 格式,并且可以安装 npm 注册表中的任何包。
¥Migrating from npm should be a fairly easy process for most users. Yarn can
consume the same package.json format as npm, and can install any package from
the npm registry.
如果你想在现有的 npm 项目上尝试 Yarn,只需尝试运行:
¥If you want to try Yarn out on your existing npm project, just try running:
yarn
这将使用与 Node.js 模块解析算法 兼容的 Yarn 解析算法来布局 node_modules 文件夹。
¥This will lay out your node_modules folder using Yarn’s resolution algorithm
that is compatible with the
node.js module resolution algorithm.
如果出现错误,请检查是否存在问题或将其报告给 Yarn 问题跟踪器。
¥If you get an error, please check for an existing issue or report it to the Yarn issue tracker.
当你运行 yarn 或 yarn add <package> 时,Yarn 将在包的根目录中生成 yarn.lock 文件。你不需要阅读或理解此文件 - 只需将其签入源代码管理即可。当其他人开始使用 Yarn 而不是 npm 时,yarn.lock 文件将确保他们获得与你完全相同的依赖。
¥When you run either yarn or yarn add <package>, Yarn will generate a yarn.lock file within the root directory of your package. You don’t need to read or understand this file - just check it into source control. When other people start using Yarn instead of npm, the yarn.lock file will ensure that they get precisely the same dependencies as you have.
大多数情况下,第一次运行 yarn 或 yarn add 就可以了。在某些情况下,package.json 文件中的信息不够明确,无法消除依赖,并且 Yarn 选择依赖的确定性方式会遇到依赖冲突。这种情况尤其可能发生在较大的项目中,有时 npm install 不起作用,开发者经常删除 node_modules 并从头开始重建。如果发生这种情况,请在转换为 Yarn 之前尝试使用 npm 使依赖的版本更加明确。
¥In most cases, running yarn or yarn add for the first time will just work. In some cases, the information in a package.json file is not explicit enough to eliminate dependencies, and the deterministic way that Yarn chooses dependencies will run into dependency conflicts. This is especially likely to happen in larger projects where sometimes npm install does not work and developers are frequently removing node_modules and rebuilding from scratch. If this happens, try using npm to make the versions of dependencies more explicit, before converting to Yarn.
从 Yarn 1.7.0 开始,你可以使用 yarn import 将 npm 生成的 导入你的 package-lock.json 状态发送到 Yarn。
¥As of Yarn 1.7.0, you can import your package-lock.json state, generated by npm to Yarn, by using yarn import.
项目中的其他开发者可以继续使用 npm,因此你不需要让项目中的每个人同时进行转换。使用 yarn 的开发者将获得完全相同的配置,而使用 npm 的开发者可能会获得略有不同的配置,这是 npm 的预期行为。
¥Other developers on the project can keep using npm, so you don’t need to get everyone on your project to convert at the same time. The developers using yarn will all get exactly the same configuration as each other, and the developers using npm may get slightly different configurations, which is the intended behavior of npm.
稍后,如果你认为 Yarn 不适合你,你可以直接返回使用 npm,而无需进行任何特定更改。如果项目中没有人再使用 Yarn,你可以删除旧的 yarn.lock 文件,但这不是必需的。
¥Later, if you decide that Yarn is not for you, you can just go back to using npm without making any particular changes. You can delete your old yarn.lock file if nobody on the project is using Yarn any more but it’s not necessary.
如果你现在使用 npm-shrinkwrap.json 文件,请注意你最终可能会得到一组不同的依赖。Yarn 不支持 npm Shrinkwrap 文件,因为它们中没有足够的信息来支持 Yarn 更具确定性的算法。如果你使用的是收缩封装文件,那么将项目中的每个人同时转换为使用 Yarn 可能会更容易。只需删除现有的 npm-shrinkwrap.json 文件并签入新创建的 yarn.lock 文件即可。
¥If you are using an npm-shrinkwrap.json file right now, be aware that you may
end up with a different set of dependencies. Yarn does not support npm
shrinkwrap files as they don’t have enough information in them to power Yarn’s
more deterministic algorithm. If you are using a shrinkwrap file it may be easier
to convert everyone working on the project to use Yarn at the same time. Simply remove
your existing npm-shrinkwrap.json file and check in the newly created yarn.lock file.
CLI 命令比较
| npm(v5) | Yarn |
|---|---|
npm install |
yarn add |
| (N/A) | yarn add --flat |
| (N/A) | yarn add --har |
npm install --no-package-lock |
yarn add --no-lockfile |
| (N/A) | yarn add --pure-lockfile |
npm install [package] --save |
yarn add [package] |
npm install [package] --save-dev |
yarn add [package] --dev |
| (N/A) | yarn add [package] --peer |
npm install [package] --save-optional |
yarn add [package] --optional |
npm install [package] --save-exact |
yarn add [package] --exact |
| (N/A) | yarn add [package] --tilde |
npm install [package] --global |
yarn global add [package] |
npm update --global |
yarn global upgrade |
npm rebuild |
yarn add --force |
npm uninstall [package] |
yarn remove [package] |
npm cache clean |
yarn cache clean [package] |
rm -rf node_modules && npm install |
yarn upgrade |
npm version major |
yarn version --major |
npm version minor |
yarn version --minor |
npm version patch |
yarn version --patch |
npm explain [package] |
yarn why [package] |
npm audit [package] |
yarn audit [package] |