NPM 命令总结

之前在下载包的时候接触了几个 npm 命令,索性将常见的命令总结一下

  1. npm adduser
    • 添加用户
  2. npm access
    • 设置包的权限
  3. npm bugs
    • 打开包的bugs 报告页面
  4. npm bin [—-global]
    • 查看npm 可执行文件的目录
  5. npm config get registry
    • 获取 npm 仓库地址
  6. npm config list [—-json]
    • 获取 npm 配置信息
  7. npm config edit
    • 编辑 npm 的配置信息
  8. npm config delete
    • 删除某个配置
    • npm c 是 npm config 别名
  9. npm cache add
    • 通过 git 地址添加某个 tar 包到缓存
  10. npm cache clean —force
    • 清除缓存文件 npm@5 后必须使用强制符
  11. npm cache verify
    • 清理无用的缓存文件和校验缓存数据完整性
  12. npm completion >> ~/.zsh
    • npm 开启自动补全命令,设置后,按住 tab 键能自动提示 npm 命令
  13. npm deprecate [@]
    • 添加对某个包的某个版本的反对信息。只有 owner 才可以,安装此包能看到这些反对信息
  14. npm dist-tag add @ []
    • 为某个版本添加 tag,如果添加的 tag 为 latest,这个 tag 版本将成为最新版本。用户下载最新的时候会直接下载到该版本
  15. npm dist-tag rm
    • 移除tag
  16. npm dist-tag ls []
    • 查看 tag 清单
  17. npm doctor
    • npm 诊断命令,npm 会对 npm 运行的环境进行检测诊断,包括 npm 的版本,git 版本,npm 使用的文件权限,npm ping 等等
  18. npm docs
    • 打开包的文档地址
  19. npm dedupe
    • 通过一些算法 将多个依赖包中重复依赖的包移除,只保留一个
    • 该命令别名为:npm ddp
  20. npm explore [ – ]
    • 进入指定的包里执行一些命令,比如执行更新源代码,执行重新编译等
  21. npm edit
    • 修改包内容
  22. npm get key val
    • 获取某个配置
  23. npm init
    • 初始化当前目录为一个包,会生成一个 package.json 的文件
  24. npm install
    • 不带任何参数,会按照根目录下的 package.json 里的依赖包
    • 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下自动生成该目录
    • 会把该包安装到 node_modules 目录中,不会修改 package.json,之后运行 npm install 命令时,不会自动安装该包
  25. npm install [<@scope>/]
    • 局部安装某个包,默认安装最新版本
  26. npm install [<@scope>/]@
    • 局部安装指定版本的某个包(按照tag)
    • 该命令的别名是:npm i
  27. npm install [<@scope>/] --save
    • 简写为:npm install [<@scope>/] -S
    • 会在 package.jsondependencies 属性下添加该包的名称及版本号,之后运行 npm install 命令时,会自动安装模块依赖到 node_modules 目录中
    • 运行 npm install --production 或者注明 NODE_ENV 变量值为 production 时,会自动下载模块到 node_modules 目录中
  28. npm install [<@scope>/] --save-dev
    • 简写为 npm install [<@scope>/] -D
    • npm install [<@scope>/] --save 的区别是:
      • --save-dev 会将模块依赖写入 devDependencies 节点,而 --save 是将模块依赖写入 dependencies 节点
      • 运行 npm install --production 或者注明 NODE_ENV 变量值为 production 时,npm install [<@scope>/] --save-dev 不会自动下载模块到 node_modules 目录中
  29. package.json 文件里面的 devDependencies 和 dependencies 对象的区别是什么呢;
    • –save-dev 对应的 devDependencies 里面的插件只用于开发环境,不用于生产环境,而 –save 对应的 dependencies 是需要发布到生产环境的
    • devDependencies 节点下的模块是开发时候需要的,如构建工具 glup、webpack,预处理器less、sass这些只在开发环境有用,所以使用 –save-dev 安装
    • dependencies 节点下的模块是线上代码运行需要的,如 axios、vue-router、vuex,所以需要使用 –-save 安装
  30. npm install-test
    • 它和 npm install 的区别是执行完 npm install 后立马执行 npm test
  31. npm install [<@scope>/] -g
    • 全局安装某个包,默认安装最新版本
  32. npm install [<@scope>/] --save
  33. npm help
    • 查看某个命令的帮助信息
  34. npm help-search
    • 在帮助文档中查找包含关键词的文档列表
  35. npm link [<@scope>/][@]
    • 将 npm 包创建快捷方式到全局 npm 的路径下
  36. npm login
    • 登陆用户
  37. npm logout
    • 退出当前登陆的 npm 账号
  38. npm ls、npm list、npm la、npm ll
    • 查看当前 npm 包的依赖包列表
    • npm lsnpm list 的简写
  39. npm outdated
    • 查看当前安装的包里版本是否过期的信息
  40. npm owner add [<@scope>/]
    • 添加某个用户作为某个包的管理者
  41. npm owner rm [<@scope>/]
    • 移除某个用户作为某个包的管理者
  42. npm owner ls [<@scope>/]
    • 查看某个包的管理者
  43. npm ping
    • 验证 registry 的连通性和身份验证
  44. npm pack
    • 将当前 npm 包文件打成一个压缩包
  45. npm prune
    • 移除没有定义在package.json 依赖配置上的包
  46. npm publish [|] [–tag ] [–access <public|restricted>]
    • 发布一个包
  47. 用户个人信息管理
    • npm profile get [–json|–parseable] []
      • 获取个人信息
    • npm profile set [–json|–parseable]
      • 设置个人信息
    • npm profile set password
      • 设置密码
    • npm profile enable-2fa [auth-and-writes|auth-only]
      • 设置开启双重验证
    • npm profile disable-2fa
      • 关闭开启双重验证
  48. npm prefix [-g]
    • 查看最靠近 package.json 的父级目录
  49. npm root [-g]
    • 查看本地或者全局 node-module 目录的位置
  50. npm repo []
    • 打开包的源文件管理地址 比如 github 地址
  51. npm restart
    • 运行当前目录下的 package.json 里的 scripts 里的 restart 命令,如果没有,则执行 npm stop 再执行 npm start
  52. npm run-script [-- …]
    • 运行当前目录下的 package.json 里的 scripts 里的命令
    • 该命令的别名是:npm run
  53. npm rebuild [[<@scope>/]…]
    • 执行编译某个包
    • 该命令的别名是:npm rb
  54. npm search [–long] [search terms …]
    • 从远程仓库中搜索某个包的信息
  55. npm set key val
    • 设置某个配置
  56. npm stop
    • 运行当前目录下的 package.json 里的 scripts 里的 top 命令,如果有的话
  57. npm start
    • 运行当前目录下的 package.json 里的 scripts 里的 start 命令,如果有的话
  58. npm star […]
    • 将某个包加入当前用户的感兴趣的清单
  59. npm stars []
    • 列出该用户所有感兴趣的包
  60. npm shrinkwrap
    • 生成当前项目确定的依赖版本,类似 package-lock.json,但是还是有略微区别
  61. npm test [-- ]
    • 运行当前目录下的 package.json 里的 scripts 里的 test 命令,如果有的话
  62. npm team
    • 管理所属团队或者添加团队成员等
  63. npm token
    • 身份令牌管理
  64. npm uninstall
    • 卸载某个包
    • 该命令的别名是:npm un
  65. npm update [-g] […]
    • 更新包
    • 该命令的别名是:npm up [-g] […]
  66. npm unpublish [<@scope>/][@]
    • 取消发布包,将发布的包从远程仓库中删除
  67. npm unstar […]
    • 将某个包移除当前用户清单
  68. npm version
    • 查看各个依赖的版本
  69. npm whoami
    • npm 当前登陆用户
  70. npm -l
    • 查看所有命令的用例信息
  71. npm -h
    • 查看某个命令的用法
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!