更详细: https://www.runoob.com/nodejs/nodejs-npm.html
因为 CentOS 7 的 C 运行库版本很低,Node 官网编译用的版本很高,所以只能用第三方编译的版本,地址如下(当然也可以自己做个 docker 镜像专门用于编译):
https://github.com/sbwml/node-latest-centos/releases/tag/v18.16.0
npm install xxx 利用 npm 安装 xxx 模块到当前命令行所在目录;
npm install -g xxx 利用 npm 安装全局模块 xxx;
本地安装时将模块写入 package.json 中:
npm install xxx 安装但不写入 package.json;
npm install xxx –save 安装并写入 package.json 的 "dependencies" 中;
npm install xxx –save-dev 安装并写入 package.json 的 "devDependencies" 中。
npm uninstall xxx 删除 xxx 模块
npm uninstall -g xxx 删除全局模块 xxx
推荐使用升级插件:
$ npm install -g npm-check-updates
// 或者
$ cnpm install -g npm-check-updates
ncu 是 npm-check-updates 的缩写命令。
可以看到有好几个包要更新
$ ncu -a # 更新
$ ncu # 查看更新
npm 自带的 npm update 和升级插件 npm-check-updates 有什么区别呢
npm update, 只能按照 package.js 中标注的版本号进行更新, 每次都要改动 package.js 中的版本号实在太麻烦。
而升级插件会自动帮你更改 package.js 里的版本号, 简单方便。
npm 清除未被使用的模块命令:
npm prune
由于网速问题, 我们需要设置一个国内镜像, 目前好像就淘宝一家了, 其它全关了。
http://npm.taobao.org 和 http://registry.npm.taobao.org 将在 2022.06.30 号正式下线和停止 DNS 解析。
域名切换规则:
# 切换为淘宝镜像命令
$ npm config set registry http://registry.npmmirror.com
# 验证是否设置成功
$ npm config get registry
# 删除 / 还原镜像
$ npm config del registry
淘宝推荐使用他们定制的 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/mirrors/node \
--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/mirrors/node \
--userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
$ cnpm install [name]
直接通过 sync 命令马上同步一个模块, 只有 cnpm 命令行才有此功能:
$ cnpm sync express
当然, 你可以直接通过 web 方式来同步: /sync/express
$ open https://npmmirror.com/sync/express
支持 npm 除了 publish 之外的所有命令, 如:
$ cnpm info express
设置代理:
npm config set proxy http://server:port
npm config set https-proxy http://server:port
# 如果代理需要认证的话可以这样来设置:
npm config set proxy http://username:password@server:port
npm config set https-proxy http://username:pawword@server:port
查看代理设置:
npm config list
清除 npm 的代理:
npm config delete proxy
npm config delete https-proxy
根据 npm phantomjs 包官网 所述:
Package renamed to phantomjs-prebuilt. Please update 'phantomjs' package references to 'phantomjs-prebuilt'
不知道从哪里修改包的引用, 所以干脆就到 phantomjs 官网 去下一个对应当前操作系统的版本, 并且添加到 PATH 中即可, 下次其它的软件就不会自动去下载并编译了。