每次从官网上下载新的Node版本,再覆盖原先的版本比较麻烦,有时很容易出错,浪费较多的时间。可以使用node的版本管理工具 n 来进行升级。
注:OS环境为CENTOS7,其他环境没有测试。
安装 n
npm install -g n
/path/node/bin/n -> /path/node/lib/node_modules/n/bin/n
+ n@6.1.3
added 1 package from 4 contributors in 1.024s
查看 n 帮助
n --help
Usage: n [options] [COMMAND] [args]
Commands:
n Display downloaded node versions and install selection
n latest Install the latest node release (downloading if necessary)
n lts Install the latest LTS node release (downloading if necessary)
n <version> Install node <version> (downloading if necessary)
n run <version> [args ...] Execute downloaded node <version> with [args ...]
n which <version> Output path for downloaded node <version>
n exec <vers> <cmd> [args...] Execute command with modified PATH, so downloaded node <version> and npm first
n rm <version ...> Remove the given downloaded version(s)
n prune Remove all downloaded versions except the installed version
n --latest Output the latest node version available
n --lts Output the latest LTS node version available
n ls Output downloaded versions
n ls-remote [version] Output matching versions available for download
n uninstall Remove the installed node and npm
Options:
-V, --version Output version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
--all ls-remote displays all matches instead of last 20
--insecure Turn off certificate checking for https requests (may be needed from behind a proxy server)
Aliases:
which: bin
run: use, as
ls: list
lsr: ls-remote
rm: -
lts: stable
latest: current
Versions:
Numeric version numbers can be complete or incomplete, with an optional leading 'v'.
Versions can also be specified by label, or codename,
and other downloadable releases by <remote-folder>/<version>
4.9.1, 8, v6.1 Numeric versions
lts Newest Long Term Support official release
latest, current Newest official release
boron, carbon Codenames for release streams
and nightly, chakracore-release/latest, rc/10 et al
n latest 安装最新版本
n lts 安装最新的LTS版本
n rm **.**.** 删除某个版本,不会删除安装文件
n uninstall 卸载并删除安装的文件
[root@localhost node]# n uninstall
Do you wish to delete node and npm from /usr/local? y
Uninstalling node and npm
/usr/local/bin/node
/usr/local/bin/npm
/usr/local/bin/npx
/usr/local/include/node
/usr/local/lib/node_modules/npm
/usr/local/share/doc/node
/usr/local/share/man/man1/node.1
/usr/local/share/systemtap/tapset/node.stp
直接运行 n lts会把node安装在/usr/local/目录下,可以通过设置 N_PREFIX 这个环境变量来修改安装node的位置。node会安装在该目录下。
[soft@localhost node]$ n lts
installing : node-v12.14.0
mkdir : /usr/local/n/versions/node/12.14.0
mkdir: cannot create directory ‘/usr/local/n’: Permission denied
Error: sudo required (or change ownership, or define N_PREFIX)
[soft@localhost application]$ export N_PREFIX=/soft/application/node
[soft@localhost application]$ echo $N_PREFIX
/soft/application/node
[soft@localhost application]$ n lts
installing : node-v12.14.0
mkdir : /soft/application/node/n/versions/node/12.14.0
fetch : https://nodejs.org/dist/v12.14.0/node-v12.14.0-linux-x64.tar.gz
installed : v12.14.0 (with npm 6.13.4)
[soft@localhost application]$ node --version
v12.14.0
这样就可以对Node进行更新。
npm的更新也可以参考这个方法:https://www.jianshu.com/p/ac6dba293ac5
项目地址为:https://github.com/felixrieseberg/npm-windows-upgrade