npm简介

维基百科中npm定义

简言之,npm是node package management,是一款包管理工具。

安装npm

Node.js安装包中包括npm,下载地址https://nodejs.org/en/download/releases/
下载node.js,例如10.11.0

安装一个包

如何使用npm进行包管理呢?

本地安装

使用如下命令,可以安装一个包

# npm install <package name>

会在当前目录下创建node-modules,将要安装的包放置在这个目录下。
并在package.json文件依赖选项中添加相应的包。

npm install 如果不带任何参数,会从package.json获取要安装的依赖包和版本,并进行安装。

全局安装

如果你想将其作为一个命令行工具,那么你应该将其安装到全局。这种安装方式后可以让你在任何目录下使用这个包。比如 grunt 就应该以这种方式安装。

# npm install -g <package name>

例如

npm install -g jshint

package.json文件

在本地目录中如果没有 package.json 这个文件的话,那么最新版本的包会被安装。
如果存在 package.json 文件,则会在 package.json 文件中查找针对这个包所约定的语义化版本规则,然后安装符合此规则的最新版本。

文件内容格式example

{
  "name": "my-awesome-package",
  "version": "1.0.0"
}

创建package.json文件

执行命令,创建package.json文件
执行过程中,会进行配置项的选择。

# mkdir my-vue-proj
# cd my-vue-proj
# npm init


This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (my-vue-proj)
version: (1.0.0)
description: npm test example
entry point: (index.js)
test command:
git repository:
keywords:
author: lanyang
license: (ISC)
About to write to /Users/zhangyunyang/workspace/my-vue-proj/package.json:

{
  "name": "my-vue-proj",
  "version": "1.0.0",
  "description": "npm test example",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "lanyang",
  "license": "ISC"
}


Is this ok? (yes) yes

查看当前目录

# ls
package.json

可以看到已经创建了package.json文件

也可以直接执行命令,创建一个默认的package.json

# npm init --yes

文件各个选项的含义

  • name: the current directory name
  • version: always 1.0.0
  • description: info from the readme, or an empty string “”
  • main: always index.js
  • scripts: by default creates an empty test script
    可以这样使用: # npm test or npm run test
  • keywords: empty
  • author: empty
  • license: ISC

package.json文件中的Dependencies

两种类型的依赖

  • “dependencies”: These packages are required by your application in production.
  • “devDependencies”: These packages are only needed for development and testing.

一个是针对生产环境的,另一个是针对测试环境。

安装一个包后,package.json文件的变化

例如

# npm install lodash

安装包后package.json文件内容如下

{
  "name": "my-vue-proj",
  "version": "1.0.0",
  "description": "npm test example",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "zyy",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.11"
  }
}

如何更新安装的包

本地安装包

执行以下两个步骤:

  • 在 package.json 文件所在的目录中执行 npm update 命令
  • 执行 npm outdated 命令,不应该有任何输出。

全局安装包

对于全局安装包,更新执行如下命令:

npm update -g <package>

例如,

npm update -g jshint

查找哪些全局包需要更新:

# npm outdated -g --depth=0.

更新所有全局的包:

npm update -g.

如何卸载安装的包

本地安装包

如需删除 node_modules 目录下面的包(package),请执行:

# npm uninstall <package>
// 例如
# npm uninstall lodash

如需从 package.json 文件中删除依赖,需要在命令后添加参数 --save:

npm uninstall --save lodash

注意:如果你将安装的包作为 “devDependency”(也就是通过 --save-dev 参数保存的),那么 --save 无法将其从 package.json 文件中删除。所以必须通过 --save-dev 参数可以将其卸载。

全局的安装包

通过如下命令将包(package)安装到全局:

# npm uninstall -g <package>

例如安装 jshint 包到全局,使用如下命令:

# npm uninstall -g jshint

package和module区别

  • A package is a file or directory that is described by a package.json. This can happen in a bunch of different ways! For more info, see "What is a package?, below.
  • A module is any file or directory that can be loaded by Node.js’ require(). Again, there are several configurations that allow this to happen. For more info, see “What is a module?”, below.

参考

npm中文文档

10-07 20:48