Node 笔记

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配置
    1. 执行命令设置镜像:npm config set registry https://registry.npmmirror.com/
    2. 验证是否生效:npm config get registry,输出目标镜像地址即成功。
    3. 恢复官方源:npm config set registry https://registry.npmjs.org/
  • 方式2:编辑.npmrc配置文件
    1. 在对应目录创建或修改文件(Windows:C:\Users\用户名;Linux/Mac:~/.npmrc)。
    2. 添加内容: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 中即可, 下次其它的软件就不会自动去下载并编译了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值