npm命令大全
待补充
1. 查看包版本
以Vue为例。
命令 | 描述 |
---|---|
npm view vue versions | 查看npm服务器上vue的所有版本信息 |
npm view vue version | 查看npm服务器上vue的最新版本信息 |
npm info vue | 查看vue全部版本信息,包含更多信息 |
npm ls vue | 查看本地安装的vue信息 |
npm ls vue -g | 查看全局安装的vue信息 |
2. 安装包
命令 | 描述 |
---|---|
npm install vue -g | 全局安装vue |
npm install vue | 本地安装vue |
npm install vue@2.6.12 | 本地安装指定版本的vue |
npm install vue --save | 本地安装vue, 将依赖包名称添加到 package.json 文件 dependencies 下 |
npm install vue --save-dev | 本地安装vue, 将依赖包名称添加到 package.json 文件 devDependencies 下 |
- --save 可以简写为 -S 是指发布之后还依赖的包;
- --save-dev 可以简写为 -D 是指开发是需要依赖的包,发布后不再需要;
3. 卸载安装包
命令 | 描述 |
---|---|
npm uninstall vue -g | 删除全局包 |
npm uninstall vue | 删除本地包 |
4. 更新安装包
- 更新指定安装包,需要提前修改package.json的安装包版本号。
npm update vue
- 全局更新:
- 查看所有可以更新的安装包
npm outdated
- 安装npm-check-updates模块
npm i npm-check-updates -g
- 查看可更新安装包
ncu
- 更新全部可更新安装包
ncu -u
可以根据实际情况添加作用范围:-D、-S 或 -g
5. 修改项目版本号
- 版本号格式:major.minor.patch
- 预设版本号格式:premajor.preminor.prepatch-prerelease
命令 | 描述 |
---|---|
npm version major | 修改主版本号 1.0.0 > 2.0.0 |
npm version minor | 修改次版本号 1.0.0 > 1.1.0 |
npm version patch | 修改补丁号 1.0.0 > 1.0.1 |
npm version premajor | 修改预设主版本号 1.0.0 > 2.0.0-0 |
npm version preminor | 修改预设次版本号 1.0.0 > 1.1.0-0 |
npm version prepatch | 修改预设补丁号 1.0.0 > 1.0.1-0 |
npm version prerelease | 修改预设发布号 1.0.0 > 1.0.0-0 |
6. npm配置
命令 | 描述 |
---|---|
npm config list | 查看基本信息,仓库地址,CMD路径,根目录等 |
npm config ls | 同上 |
npm config ls -l | 查看npm全部信息 |
npm config get key | 查看npm某项配置信息,key:配置名称 |
npm config set key value | 设置某配合信息,key: 配置名称; value: 配置值 |
6.1 切换仓库到淘宝镜像
// 永久切换
npm config set registry https://registry.npm.taobao.org
// 验证切换是否成功
npm config get registry
// 一次性切换
npm i vue --registry https://registry.npm.taobao.org
6.2 添加cnpm指令
npm i cnpm -g --registry https://registry.npm.taobao.org
7. 发包
将包发布到npmjs公共仓库上。
7.1. 注册npm用户
官网地址:http://registry.npmjs.org
7.2 登录npm
7.2.1 第一次发包
npm adduser
username: 用户名
password:密码
email:邮箱
7.2.2 非第一次发包
npm login
username: 用户名
password:密码
email:邮箱
7.3 发包
npm publish
7.4 处理错误信息
7.4.1 E403
- 需要验证一下注册的邮箱
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT http://registry.npmjs.org/xxxxxx
- you must verify your email before publishing a new package:
https://www.npmjs.com/email-edit
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.
- 修改package.json中的 versions 版本号
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT http://registry.npmjs.org/xxxxxxx
- You cannot publish over the previously published versions: 0.0.2.
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.
- 非官方源,
切换到官方源: http://registry.npmjs.org
切换之后需要重新登录。
npm ERR! 403 Forbidden - PUT http://registry.npm.taobao.com/xxxxxxx - no_perms
- 报名重复
npm ERR! 403 Forbidden - PUT https://registry.npmjs.org/xxxxx
- You do not have permission to publish "xxxxx".
Are you logged in as the correct user?