npm 常用命令

发布时间: 更新时间: 总字数:958 阅读时间:2m 作者: 分享 复制网址
专栏文章
  1. nodejs 版本管理
  2. NPM Registry Proxy 配置
  3. npm 常用命令(当前)
  4. 使用 forever 运行 nodejs 应用

npm 其实是 Node.js 的包管理工具(node package manager),本文介绍 npm 常用命令。

安装

安装 nodejs

  • mac
brew install node

验证:

node -v
npm -v

npm

npm 其实是 Node.js 的包管理工具(package manager)。

查看配置:

npm config ls -l

手动切换

npm config set registry https://registry.npmmirror.com/

cnpm

npm 源由于被墙,可以试用淘宝的 nodejs 源。使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

$ npm install -g cnpm --registry=https://registry.npmmirror.com

或者你直接通过添加 npm 参数 alias 一个新命令:

alias cnpm="npm --registry=https://registry.npmmirror.com \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npmmirror.com/package/dist \
--userconfig=$HOME/.cnpmrc"
# Or alias it in .bashrc or .zshrc
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npmmirror.com \
  --cache=$HOME/.npm/.cache/cnpm \
  --disturl=https://npmmirror.com/package/dist \
  --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc

同步模块直接通过 sync 命令马上同步一个模块, 只有 cnpm 命令行才有此功能:

$ cnpm sync express

安装模块

从 registry.npmmirror.com 安装所有模块. 当安装的时候发现安装的模块还没有同步过来, 淘宝 NPM 会自动在后台进行同步, 并且会让你从官方 NPM registry.npmjs.org 进行安装. 下次你再安装这个模块的时候, 就会直接从 淘宝 NPM 安装了.

$ cnpm install [name]

nrm

nrm(NPM registry manager) 是 npm 的镜像源管理工具,用来快速切换 npm 源

# 安装
npm i -g nrm

# 帮助
nrm -h

# 版本
nrm -V

# 查看所有源
$ nrm ls
  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/

 # 切换源 nrm use <registry_name>
 nrm use taobao

 # 添加源
nrm add <registry_name> <url>

# 删除源
$ nrm del <registry_name>

# 测试源

$ nrm test <registry_name>

yarn & tyarn

  • yarn 支持并行下载的,下载速度很快
  • tyarn 使用 npmmirror.com 作为源
npm install yarn tyarn -g

npm 常用命令

npm 的命令适用于 cnpm、yarn和tyarn

  • 帮助,如果要单独查看install命令的帮助,可以使用的npm help install
npm help
  • 安装nodejs的依赖包
npm install <name>

例如:npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本,如npm install express@3.0.6

  • 将包安装到全局环境中
npm install <name> -g

但是代码中,直接通过require()的方式是没有办法调用全局安装的包的。全局的安装是供命令行使用的,就好像全局安装了vmarket后,就可以在命令行中直接运行vm命令

  • 安装的同时,将信息写入package.json中
npm install <name> --save

项目路径中如果有package.json文件时,直接使用npm install方法就可以根据dependencies配置安装所有的依赖包

这样代码提交到github时,就不用提交node_modules这个文件夹了。

  • 会引导你创建一个package.json文件,包括名称、版本、作者这些信息等
npm init
  • 移除
npm remove <name>

-更新

npm update <name>
  • 列出当前安装的了所有包
npm ls
  • 查看当前包的安装路径
npm root
  • 查看全局的包的安装路径
npm root -g

F&Q

fetch headers ssl err

  • 错误日志
gyp ERR! stack FetchError: request to https://nodejs.org/download/...headers.tar.gz failed, reason: unable to get local issuer certificate
  • 解决方式
cd /tmp && wget https://nodejs.org/download/release/v16.13.2/node-v16.13.2-headers.tar.gz
npm config set tarball /tmp/node-v16.13.2-headers.tar.gz
npm ci
专栏文章
  1. nodejs 版本管理
  2. NPM Registry Proxy 配置
  3. npm 常用命令(当前)
  4. 使用 forever 运行 nodejs 应用
最新评论
加载中...
Home Archives Categories Tags Statistics