Skip to content

npm common commands ​

Introduction ​

npm is a package (module) manager installed with node.Common usage scenarios include the following:

  • Allow users to download third-party packages written by others from the npm server for local use.
  • Allow users to download and install command line programs written by others from the npm server for local use.
  • Allows users to upload packages or command line programs they write to the npm server for others to use.

Common commands ​

Check whether it is installed and version ​

sh
npm -v # 显示版本号说明已经安装相应的版本

Generate package.json file ​

sh
npm init

package.json is used to describe the modules and other information used in the project

Install module ​

sh
npm install # 安装package.json定义好的模块,简写 npm i

# 安装包指定模块
npm i <ModuleName>

# 全局安装
npm i <ModuleName> -g 

# 安装包的同时,将信息写入到package.json中的 dependencies 配置中
npm i <ModuleName> --save

# 安装包的同时,将信息写入到package.json中的 devDependencies 配置中
npm i <ModuleName> --save-dev

# 安装多模块
npm i <ModuleName1> <ModuleName2>

# 安装方式参数:
-save # 简写-S,加入到生产依赖中
-save-dev # 简写-D,加入到开发依赖中
-g # 全局安装 将安装包放在 /usr/local 下或者你 node 的安装目录

View ​

sh
# 查看所有全局安装的包
npm ls -g

# 查看本地项目中安装的包
npm ls

# 查看包的 package.json文件
npm view <ModuleName>

# 查看包的依赖关系
npm view <ModuleName> dependencies

# 查看包的源文件地址
npm view <ModuleName> repository.url

# 查看包所依赖的node版本
npm view <ModuleName> engines

# 查看帮助
npm help

Update module ​

sh
# 更新本地模块
npm update <ModuleName>

# 更新全局模块
npm update -g <ModuleName> # 更新全局软件包。
npm update -g # 更新所有的全局软件包。
npm outdated -g --depth=0 # 找出需要更新的包。

Uninstall module ​

sh
# 卸载本地模块
npm uninstall <ModuleName>

# 卸载全局模块
npm uninstall -g <ModuleName> # 卸载全局软件包。

Clear cache ​

sh
# 清空npm缓存
npm cache clear

Use Taobao mirror ​

sh
# 使用淘宝镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org

Others ​

sh
# 更改包内容后进行重建
npm rebuild <ModuleName>

# 检查包是否已经过时,此命令会列出所有已经过时的包,可以及时进行包的更新
npm outdated

# 访问npm的json文件,此命令将会打开一个网页
npm help json

# 发布一个包的时候,需要检验某个包名是否存在
npm search <ModuleName>

# 撤销自己发布过的某个版本代码
npm unpublish <package> <version>

Tips ​

If the installation fails many times, try clearing the cache first. ​

sh
npm cache clean -f

View installed dependency package version numbers ​

sh
npm ls <ModuleName>

Note: Only by using this method can you accurately know the version number used by the project. When viewing package.json, there is a "^" symbol indicating that it is greater than this version.

npm release package tutorial ​

npm release package tutorial

The function and use of nrm ​

What is nrm? ​

nrm (npm registry manager) is npm's image source management tool. Sometimes foreign resources are too slow. You can use this to quickly switch between npm sources.

nrm installation ​

sh
npm install -g nrm

nrm command ​

sh
nrm ls #查看可用的源(有*号的表示当前所使用的源,以下<registry>表示源的名称)
nrm use <registry> # 将npm下载源切换成指定的源
nrm add <registry> <url> # 添加源,url为源的路径
nrm del <registry> # 删除源
nrm test <registry> # 测试源的响应时间,可以作为使用哪个源的参考

nrm help # 查看nrm帮助
nrm home <registry> # 跳转到指定源的官网

nrm use ​

nrm is very useful if your network is not ideal or is subject to other network restrictions that prevent you from using npm's original source for downloading. You only need to:

sh
nrm ls # 查看可用的源
nrm use <registry> # 切换到指定源