「本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战!」
npm
是随同 NodeJS
一起安装的包管理工具,能解决 NodeJS
代码部署上的很多问题,常见的使用场景有以下几种:
- 允许用户从
npm
服务器下载别人编写的第三方包到本地使用。 - 允许用户从
npm
服务器下载并安装别人编写的命令行程序到本地使用。
- 允许用户将自己编写的包或命令行程序上传到
npm
服务器供别人使用。
常用命令
# 查看 npm 的版本
npm -v
# 查看所有命令
npm help
# OR 查看某条命令的详细帮助
npm help <command>
# 查看各个命令的简单用法
npm -l
npm init 初始化
使用 npm init
生成 package.json
文件
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 init` 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: (test)
# 版本号
version: (1.0.0)
# 项目描述
description:
# 入口文件
entry point: (index.js)
# 运行 test 脚本时所执行的命令
test command:
# 项目 git 地址
git repository:
# 关键字,用于 npm search
keywords:
# 作者
author:
# 开源协议
license: (ISC)
说明: 可以使用 npm help package-json
命令查看具体字段的说明
npm config 配置文件
# 查看 npm 的配置
npm config list -l
# 查看 npm 配置
npm config get init.author.name
# 修改 npm 默认配置
npm config set init.author.name maomao
# 删除 npm 指定配置
npm config delete init.author.name
# 别名
c
npm search 搜索模块
npm search
命令用于搜索 npm
仓库
npm search 搜索词 [-g]
# 别名
find
s
se
npm ls 查看已安装的模块
# 查看当前项目安装的所有模块
npm ls
# 查看全局安装的所有模块
npm ls -g --depth 0
# 别名
list
la
ll
npm view 查看模块详细信息
npm view lodash
# 别名
info
show
v
npm install 模块安装
# 安装最新包
npm install lodash
# 安装指定版本
npm install lodash@4.17.20
# 安装指定版本范围
npm install lodash@">=4.17.1 <4.17.21"
# 通过 git 地址安装
npm install https://github.com/lodash/lodash
# 别名
i add
# 配置项
# 全局依赖
--global -g
# 生产环境依赖 dependencies (默认)
--save -S
# 开发环境依赖 devDependencies
--save-dev -D
npm uninstall 模块卸载
# 卸载
npm uninstall lodash
# 查看模块
npm ls lodash
npm update 模块更新
# 查看查看当前是否有过期的模块包
npm outdated
# 更新指定或所有过期的模块包
npm update
# OR
npm update lodash
update
命令只会更新 package-lock.json
文件,不会更新 package.json
文件中模块包的版本号
如需更新版本号,可以使用 npm-check-updates 工具包
# 安装
npm install -g npm-check-updates
# 检查所有依赖包版本
ncu
# 检查指定包版本
ncu axios
# 直接升级所有依赖包版本
ncu -u
# 升级指定包版本
ncu -u axios
# 询问升级依赖包版本
ncu -i
# 升级指定包版本
ncu -i axios
# 指定版本范围
ncu --target minor
# OR
ncu --target patch
npm link 引用模块
npm
本地包的开发与调试
# 在全局模块路径下中创建 link
npm link
# 在其他目录下创建一个从全局模块路径到项目模块路径的 link
# packageName 是取自包的 package.json 中 name 字段
npm link packageName
# 别名
ln
# 移除模块引用
npm unlink
npm run-script 执行脚本
package.json
文件中的 scripts
字段,可以用于指定脚本命令供 npm
直接调用
npm run-script test
# 别名
run
rum
urn
内置预定义命令
在使用内置预定义命令时可以不用添加 run
npm start
npm restart
npm test
npm stop
命令串行与并行
# 并行执行 & 同时的平行执行
npm run script1 & npm run script2
# 继发执行 && 前一个任务成功,才执行下一个任务
npm run script1 && npm run script2
发布相关命令
# 在 npmjs.com 注册一个用户
npm adduser
# 登录
npm login
# 将当前模块发布到 npmjs.com
npm publish
其他命令
# 获取当前项目的 node_modules 路径
npm root
# 获取当前项目的可执行脚本路径(.bin 文件夹的路径)
npm bin
# 清除 npm 缓存 force 强制
npm cache clean --force
# 打开当前项目或者指定模块的文档
npm docs [package-name]
# 别名
home
# 打开当前项目或者指定模块的 GitHub repo
npm repo [package-name]
# 打开当前项目或者指定模块的 GitHub issues
npm bugs [package-name]
# 别名
issues
# 移除无关的模块(package.json 中没有的)
npm prune
# 锁定当前项目的依赖模块的版本
npm shrinkwrap
钩子
在 npm
脚本中有 pre
和 post
两个钩子
比如 build
脚本命令的钩子就是 prebuild
和 postbuild
{
"scripts": {
"prebuild": "echo I run before the build script",
"build": "echo build",
"postbuild": "echo I run after the build script"
}
}
当用户执行 npm run build
时会按如下顺序执行
npm run prebuild && npm run build && npm run postbuild
默认钩子
prepublish
postpublish
preinstall
postinstall
preuninstall
postuninstall
preversion
postversion
pretest
posttest
prestop
poststop
prestart
poststart
prerestart
postrestart
注意
- 自定义的脚本命令也可以加上
pre
和post
钩子
比如deploy
也有predeploy
和postdeploy
钩子 - 双重的
pre
和post
无效,
比如prepretest
和postposttest