使用npm安装yarn
One of the beautiful things about both Open Source and the ecosystems of modern programming languages is that there’s a good chance the code you’re about to write has already been written.
关于开放源代码和现代编程语言生态系统的美丽之处之一是,您将要编写的代码很有可能已经被编写。
There are a plethora of packages out there for Node.js and between you and me, they are usually written by folks smarter than myself that have thought through of a bunch of stuff I wouldn’t have even dreamed of. Standing on the shoulders of giants, as they say.
有大量的Node.js软件包,在你我之间,它们通常是由比我聪明的人编写的,这些人想到了很多我什至没有想到的东西。 正如他们所说,站在巨人的肩膀上。
入门 (Getting started)
For this article, I’m going to discuss using both npm
and yarn
. If you’re an avid reader of our reptilian friendly site, you have probably seen both commands mentioned in our other Node.js articles.
对于本文,我将讨论同时使用npm
和yarn
。 如果您是我们的爬虫友好站点的狂热读者,您可能已经在其他Node.js文章中看到了这两个命令。
For those new to the party, npm
and yarn
are package managers for Node.js. They both leverage the package.json file for your projects and function quite similarly.
对于npm
参加聚会的人来说, npm
和yarn
是Node.js的程序包管理器。 它们都将package.json文件用于您的项目,并且功能非常相似。
If you already have Node.js installed locally, you probably have npm
installed. If you’d prefer to follow along using yarn
, you can check out their installation instructions here.
如果您已经在本地安装了Node.js,则可能已经安装了npm
。 如果您希望继续使用yarn
,可以在此处查看其安装说明。
Depending on your system, you could also consult with your friendly neighborhood package manager and install things that way.
根据您的系统,您还可以咨询友好的社区包管理器并以这种方式进行安装。
Also, we’re going to be installing things globally as well as to a project as a dependency. You could very well use an existing project of yours, or you could create a dummy project out in your /tmp
directory as such:
另外,我们将在全球范围内安装东西,并将其作为依赖项安装到项目中。 您可以很好地使用您现有的项目,也可以在/tmp
目录中创建一个虚拟项目,如下所示:
$ mkdir /tmp/gator-project
$ cd /tmp/gator-project
$ npm init -y
This creates a package.json
file that we will be adding and removing packages from.
这将创建一个package.json
文件,我们将在其中添加和删除软件包。
将开发依赖项添加到项目中 (Adding a Development Dependency to a Project)
Not all dependencies are created equal, as some are only required while doing development. These dependencies, while important, can slow down production deployments since they take time to install and the code will never be touched.
并非所有的依赖关系都是平等创建的,因为某些依赖关系仅在开发时才需要。 这些依存关系虽然很重要,但会降低生产部署的速度,因为它们需要花费一些时间来安装,并且永远都不会碰到代码。
Examples of development dependencies would be testing utilities like mocha
or jest
. For those types of dependencies, we can install them as such, and have them added to the devDependencies
section of our package.json
:
开发依赖项的示例是测试实用程序,例如mocha
或jest
。 对于这些类型的依赖关系,我们可以按原样安装它们,并将其添加到package.json
的devDependencies
部分中:
# With NPM
$ npm install --save-dev mocha
# Shorthand version
$ npm i -D mocha
# With Yarn
$ yarn add --dev mocha
# Shorthand version
$ yarn add -D mocha
将生产依赖项添加到项目中 (Adding a Production Dependency to a Project)
Other dependencies are mission critical to the application and should always be installed regardless if it’s a development environment or not. We call these production dependencies and tend to includes packages like express
or react
.
其他依赖关系对应用程序来说是至关重要的,无论是否为开发环境,都应始终安装。 我们称这些为生产依赖性,并且倾向于包含诸如express
或react
包。
Adding a production dependency to a project is just as easy as adding a development one, but it will be added to the dependencies
section of our package.json
instead:
向项目中添加生产依赖项与添加开发项目一样简单,但是它将被添加到我们的package.json
的dependencies
部分中:
# With NPM
$ npm install --save express
# Shorthand version
$ npm i -P express
# With Yarn
$ yarn add express
全局安装软件包 (Installing a Package Globally)
Sometimes you want to install a package outside of your current project, so it’s available to all of the projects on your system. These are installed globally and are great for packages that also include command-line utilities that you want to run alongside your other command-line utilities:
有时您想在当前项目之外安装软件包,因此该软件包可用于系统上的所有项目。 这些是在全球范围内安装的,非常适合包含您要与其他命令行实用程序一起运行的命令行实用程序的软件包:
# With NPM
$ npm install --global json
# Shorthand version
$ npm i -g json
# With Yarn
$ yarn global add json
从项目中删除依赖项 (Removing a Dependency From a Project)
In every project’s life, there comes a time when a dependency that once seemed like a good idea, no longer serves any purpose. Don’t be too sad, deleting code is always a good thing (assuming you have proper test cover to ensure you didn’t break anything).
在每个项目的生命中,总会有一个曾经似乎是个好主意的依赖不再起作用的时代。 不要太难过,删除代码始终是一件好事(假设您具有适当的测试覆盖范围以确保您没有破坏任何东西)。
To remove either a development or production dependency from a project, we simply uninstall or remove it:
要从项目中删除开发或生产依赖项,我们只需卸载或删除它:
# With NPM
$ npm uninstall jest
# Shorthand version
$ npm r jest
# With Yarn
$ yarn remove jest
This will remove things from node_modules
as well as drop the dependency from our package.json
. Depending on your version of either command, you may also see updates to your lock file.
这将从node_modules
删除内容,并从我们的package.json
删除依赖项。 根据任一命令的版本,您可能还会看到锁定文件的更新。
全局卸载软件包 (Uninstalling a Package Globally)
Removing a globally installed package is the same as removing one from a project, but we need to pass in the global argument as we did when installing it:
删除全局安装的软件包与从项目中删除一个软件包相同,但是我们需要像安装它时一样传入全局参数:
# With NPM
$ npm uninstall --global json
# Shorthand version
$ npm r -g json
# With Yarn
$ yarn global remove json
翻译自: https://www.digitalocean.com/community/tutorials/nodejs-adding-removing-packages-npm-yarn
使用npm安装yarn