目录
1. Node 笔记
更详细: https://www.runoob.com/nodejs/nodejs-npm.html
1.1. 安装
1.1.1. CentOS 7 安装
因为 CentOS 7 的 C 运行库版本很低,Node 官网编译用的版本很高,所以只能用第三方编译的版本,地址如下(当然也可以自己做个 docker 镜像专门用于编译):
https://github.com/sbwml/node-latest-centos/releases/tag/v18.16.0
1.2. 安装模块
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" 中。
1.3. 删除模块
npm uninstall xxx 删除 xxx 模块
npm uninstall -g xxx 删除全局模块 xxx
1.4. 升级模块
推荐使用升级插件:
$ 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 里的版本号, 简单方便。
1.5. 清理模块
npm 清除未被使用的模块命令:
npm prune
1.6. 设置淘宝镜像
由于网速问题, 我们需要设置一个国内镜像, 目前好像就淘宝一家了, 其它全关了。
http://npm.taobao.org 和 http://registry.npm.taobao.org 将在 2022.06.30 号正式下线和停止 DNS 解析。
域名切换规则:
- http://npm.taobao.org => http://npmmirror.com
- http://registry.npm.taobao.org => http://registry.npmmirror.com
# 切换为淘宝镜像命令
$ npm config set registry http://registry.npmmirror.com
# 验证是否设置成功
$ npm config get registry
# 删除 / 还原镜像
$ npm config del registry
🔍 核心背景:为何用淘宝镜像
- npm默认使用官方源(https://registry.npmjs.org/),国内访问时易出现速度慢或频繁超时的情况。
- 淘宝NPM镜像(现更名为npmmirror.com)提供国内高速服务,地址为https://registry.npmmirror.com/。
🛠️ 具体使用方法
1. 临时使用(单次有效)
- 仅在安装特定包的命令中添加
--registry参数,不影响其他npm操作。 - 示例命令:
npm install express --registry=https://registry.npmmirror.com/
2. 永久使用(两种方式)
- 方式1:修改npm配置
- 执行命令设置镜像:
npm config set registry https://registry.npmmirror.com/ - 验证是否生效:
npm config get registry,输出目标镜像地址即成功。 - 恢复官方源:
npm config set registry https://registry.npmjs.org/
- 执行命令设置镜像:
- 方式2:编辑.npmrc配置文件
- 在对应目录创建或修改文件(Windows:C:\Users\用户名;Linux/Mac:~/.npmrc)。
- 添加内容:
registry=https://registry.npmmirror.com/,可通过项目内单独文件覆盖全局配置。
3. 可选工具:安装cnpm
- cnpm是淘宝镜像提供的命令行工具,用法与npm类似,下载速度更快。
- 安装命令:
npm install -g cnpm --registry=https://registry.npmmirror.com/ - 使用示例:
cnpm install express,且不影响原有npm的使用。
💡 常用辅助技巧
- 查看当前镜像源:
npm config get registry - 清理npm缓存:
npm cache clean --force - 升级npm(通过淘宝镜像加速):
npm install -g npm --registry=https://registry.npmmirror.com/
1.6.1. cnpm
淘宝推荐使用他们定制的 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
1.7. 设置代理
设置代理:
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
1.8. 问题 / 报错
1.8.1. phantomjs 这个 npm 包已经废弃, 不再使用了
根据 npm phantomjs 包官网 所述:
Package renamed to phantomjs-prebuilt. Please update 'phantomjs' package references to 'phantomjs-prebuilt'
不知道从哪里修改包的引用, 所以干脆就到 phantomjs 官网 去下一个对应当前操作系统的版本, 并且添加到 PATH 中即可, 下次其它的软件就不会自动去下载并编译了。
4360

被折叠的 条评论
为什么被折叠?



