1 结论
yarn比较好用,很多情况npm安装报错,但是yarn安装没有问题。
想详细了解的参考文章Yarn vs NPM: A Comprehensive Comparison
2 命令比较
Command | NPM | Yarn |
---|---|---|
Initialize a project | npm init | yarn init |
Run tests for the current package | npm test | yarn test |
Check for outdated packages | npm outdated | yarn outdated |
Publish a package | npm publish | yarn publish |
Run a script | npm run | yarn run |
Manage local package cache | npm cache clean | yarn cache clean |
Log in or out | npm login/logout | yarn login/logout |
Install dependencies | npm install | yarn |
Install packages | npm install [package name] | yarn add [package name] |
Uninstall packages | npm uninstall [package name] | yarn remove [package name] |
Update manager | npm update | yarn upgrade |
Update packages | npm update [package name] | yarn upgrade [package name] |
Install packages globally | npm install --global [package name] | yarn global add [package name] |
Uninstall packages globally | npm uninstall --global [package name] | yarn global remove [package name] |
Interactive dependency update | npm run upgrade-interactive | yarn upgrade-interactive |
Run package remotely | yarn dlx | |
Check licenses | yarn licenses ls |
3 优缺点
Yarn vs. NPM: 如何选择
It’s essential to consider the advantages and disadvantages of both NPM and Yarn when deciding which one to use.
Yarn
Advantages
- Supports parallel installation and Zero installs, both of which dramatically increase performance.
- Newer versions of Yarn offer a more secure form of version locking.
- Active user community.
Disadvantages
- Yarn doesn’t work with Node.js versions older than version 5.
- Yarn has shown problems when trying to install native modules.
NPM
Advantages
- Easy to use, especially for developers used to the workflow of older versions.
- Local package installation is optimized to save hard drive space.
- The simple UI helps reduce development time.
Disadvantages
- The online NPM registry can become unreliable in case of performance issues. This also means that NPM requires network access to install packages from the registry.
- Despite a series of improvements across different versions, there are still security vulnerabilities when installing packages.
- Command output can be difficult to read.
Verdict
Considering all of the above, NPM is better for developers that are used to it and satisfied with its current workflow. It provides a reasonably efficient user experience while also saving hard drive space.
On the other hand, Yarn offers more advanced features, such as Plug’n’Play and Zero installs. It also provides slightly better performance and security but at the cost of hard drive space.
Conclusion
After reading this tutorial, you should have a clearer understanding of what Yarn and NPM have to offer. Hopefully, this will help you choose the package manager that best suits your needs.