本文汇聚了基于 node 开发过程中会用到的 CLI 工具,主要包括:npm 镜像管理工具、node 版本管理工具、node_modules 命令执行工具
nvm - node 版本管理
安装
安装方式请参照官方仓库
使用
# 查看所有命令
nvm
# 查看现在使用的node版本
nvm ls
# 安装指定版本的 node
nvm install 16.13.2
# 查看node所有可安装版本
nvm ls-remote
# 使用指定版本 node。nvm 会根据指定的版本号智能选择 node 版本
# 例如,本地安装了 V16.13.2,当执行 `nvm use 16` 时,nvm 会自动引用 16.13.2,当有多个 v16 版本时,优先使用更新的版本
nvm use node版本号
# 卸载指定版本node
nvm uninsstall node版本号
nrm - 管理npm 镜像
安装
npm install -g nrm
使用
# list all supported repos
nrm ls
# add new public(or private) repo
nrm add name repoURL repoHomePage
# switch to specified repo
nrm use name
# list the repo info used for now
nrm current
npx - 方便执行 node_modules 中的命令
在开发过程中,难免会使用到仅在开发阶段使用到的工具,如:tsc
。一般我们会使用 npm i typescript
将 ts 相关工具安装到项目中,这样既方便项目中对工具的版本管理,也有利于防止污染操作系统的环境。一般情况下,如果要使用 tsc 编译项目中的 ts
文件,需要这样写:./node_modules/.../tsc tsfile.ts
。使用起来极不方便。有了 npx,上面的命令就可以简化为:npx tsc tsfile.ts