Node最近几年可谓风光无限,让前端工程师们逆袭成为「全栈工程师」,也使JavaScript这门动态语言一雪前耻,当年对其的重视相当不够,很多人甚至觉得它是个美丽的错误。大量开发者的贡献使Node版本的迭代速度很快,版本很多,所以升级Node版本就成为了一个问题。目前有n、nvm和nvmw这两个工具可以对Node进行无痛升级
**n**
mac和Linux可用的node版本管理工具
n是Node的一个模块,作者是TJ Holowaychuk(鼎鼎大名的Express框架作者),就像它的名字一样,它的理念就是简单:
“no subshells, no profile setup, no convoluted api, just simple”
安装很简单:
$ sudo npm install -g n
安装完成之后,直接输入n后输出当前已经安装的node版本以及正在使用的版本(前面有一个o),你可以通过移动上下方向键来选择要使用的版本,最后按回车生效。
$ n
0.10.1
0.10.15
o 0.10.21
0.11.8
如果你要安装其他的版本(比如0.11.12),那么如下:
$ n 0.11.12
install : 0.11.12
mkdir : /usr/local/n/versions/0.11.12
fetch : http://nodejs.org/dist/v0.11.12/node-v0.11.12-darwin-x64.tar.gz
安装最新的版本
$ n latest
安装稳定版本
$ n stable
删除某个版本
$ n rm 0.10.1
以指定的版本来执行脚本
$ n use 0.10.21 some.js
**nvm**
针对mac的node版本管理工具
nvm全称Node Version Manager,它与n的实现方式不同,其是通过shell脚本实现的。
安装方式有两种:
$ curl https://raw.github.com/creationix/nvm/v0.4.0/install.sh | sh
或者
$ wget -qO- https://raw.github.com/creationix/nvm/v0.4.0/install.sh | sh
以上脚本会把nvm库clone到~/.nvm,然后会在~/.bash_profile, ~/.zshrc或`~/.profile末尾添加source,安装完成之后,你可以用以下命令来安装node
$ nvm install 0.10
使用指定的版本
$ nvm use 0.10
查看当前已经安装的版本
$ nvm ls
.nvm
-> v0.10.24
查看正在使用的版本
$ nvm current
v0.10.24
以指定版本执行脚本
$ nvm run 0.10.24 myApp.js
卸载nvm
$ rm -rf ~/.nvm
**nvmw**
针对windows的node版本管理工具
以下是其对应的readme.md文件
‘
This is a simple Node Version Manager for Windows
Installation
npm install -g nvmw
Usage
nvmw -h
Usage: nvmw [options] [command]
Commands:
install <version> install the given version of Node
uninstall <version> uninstall the given version of Node
use <version> use the given version of Node in current shell
deactivate undo effects of nvmw in current shell
switch <version> permanently use the given version of Node as default
switch-deactivate permanently undo effects of nvmw
ls list the installed all Nodes
ls-remote list remote versions available for install
cleanup remove stale local caches
Options:
-h, --help output usage information
-V, --version output the version number
Examples:
nvmw install v0.10.20
nvmw uninstall v0.10.20
nvmw use v0.10.20
Notes
* It doesn’t work in Windows Powershell
* The latest version for npm is 1.4.12
* You should install a system version Node with Windows installer
* Node versions will be installed on the path X:\Users\\AppData\Roaming\nvm\
License
MIT
’
**nodist**
A node.js and npm version manager for the windows folks out there.
以下是nodist的readme.md文件
A node.js and npm version manager for the windows folks out there. Inspired by n. And nodenv.
Heads up! Nodist v0.8 is here! Nodist supports cmd, Powershell, Git bash and Cygwin!
C:\> nodist + 5
5.11.0
C:\> nodist global 5
5
C:\> node -v
v5.11.0
C:\> nodist
0.10.26
4.4.3
> 5.11.0 (global: 5)
(see Usage)
Nodist was designed to replace any existing node.js installation, so if node is already installed on your machine, uninstall it first.
For this you’ll need chocolatey, of course.
choco install nodist
Git bash integration is setup automatically.
Before you are able to use nodist in cygwin you need to run the following in your cygwin terminal after installing nodist:
cat "$NODIST_PREFIX/bin/nodist_bash_profile_content.sh" >> ~/.bash_profile
source ~/.bash_profile
You might need to ‘Unblock’ the file bin\nodist.ps1
by right clicking on it in the Explorer and selecting that menu entry.
If your IDE cannot access node or npm right away, don’t fret! Find the relevant configuration settings and either set path\to\Nodist\bin
as the Node installation dir or set path\to\Nodist\bin\node.exe
as the path to the node binary directly.
Similar for npm: either set ...\Nodist\bin
as the installation path or ...\Nodist\bin\npm.cmd
directly. Some IDEs (eg. WebStorm) require you to set the path to an npm package containing bin\npm-cli.js
. Nodist comes with a proxy npm folder: Simply set ...\Nodist\npm
as the npm package (this feature is experimental and feedback is appreciated!).
If you’re looking to upgrade your Nodist installation, the easiest way is to uninstall (see below) the old installation and install the new version with the installer above.
You may need to reset your per-directory node version settings in order for them to work in v0.8 (ones set using nodist v0.6 should work fine, ones set using v0.7 will not).
Starting in v0.8 Nodist employs lazy version pattern evaluation. This means that setting versions per env/locally/globally doesn’t set an explicit version, if you didn’t give one. Instead the node.exe shim chooses a suitable version at runtime. Commands for installing a version and setting a requirement have been separated, thus, to update your node version (if your global version is set to 6
, e.g.), you now need to run nodist + 6
(i.e. nodist 6
doesn’t do that for you anymore), which is probably how it should have worked all along.
Run the uninstaller either from the directory where you installed nodist, or from the Software Control Panel
Make sure to completely remove the nodist directory. (This will remove all your globally installed npm modules. If you don’t want that, remove everything else, other than nodist\bin\
.)
Remove <..path..>\nodist\bin
from your path. (how?).
Remove the NODIST_PREFIX
env var.
Delete the nodist directory
Run npm config delete prefix
on the command prompt to restore NPM functionality if you are going back to the official node install.
Nodist allows you to set version requirements for different scopes.
Version requirements can be fully specified versions, like 4.0.0
or patterns like 0.12
, 4.x
, ~5
or latest
.
io.js is supported natively: Since node and io.js versions form a continuum you can simply use io.js versions as if they were node versions.
Setting a requirement installs a matching version only if there is no other matching version already installed; otherwise existing installed versions will get referenced when executing node.exe
.
The following is a list of all scopes ordered by precedence (the first scope is the one with the highest priority; only if it’s not set, the second scope is examined).
NODIST_NODE_VERSION
and NODIST_NPM_VERSION
env vars)package.json
with an engines
field in the directory of interest or one of its parent directories).node-version
or .npm-version
in the directory of interest or one of its parent directories)When you’re just running node, the directory of interest is the directory of the javascript file to be executed. When running npm, it is the current working directory.
Any instances of node invoked by npm will be locked to the same version npm runs on.
Package.json inspection is turned off by default as of nodist v0.8.5. You can turn it on by setting NODIST_INSPECT_PACKAGEJSON=1
.
All commands automatically install the latest matching version before setting the version pattern.
> nodist
# Lists installed versions highlighting the active ones.
> nodist global 4.x
# Sets the global node version requirement
> nodist local 4.x
# Sets the node version requirement per directory (including all subdirectories).
> nodist env 4.x
# Sets the node version requirement per terminal.
> nodist global npm 3.x
# Set global npm version requirement.
> nodist npm global match
# Tell nodist to always choose the npm version that matches the current node version.
# (the current node version may be determined by env, local or global requirements)
> nodist npm local 2.x
# Set the npm version requirement for the current directory.
> nodist npm env 2.x
# Set the npm version requirement for the current terminal environment.
call nodist env 4.x
# In a batch script use `call`.
> nodist dist
# Lists all available node versions.
> nodist + 4.x
# Just checks, if the version is installed and installs it if not.
> nodist + all
# will install *everything*.
> nodist - 4.1.1
# Removes a version.
> nodist --help
# Displays a complete list of commands with examples.
> set HTTP_PROXY=http://myproxy.com:8213
# Set a proxy to use for fetching the executables
# (you may also use `HTTP_PROXY`/`http_proxy`/`HTTPS_PROXY`/`https_proxy`).
> set NODIST_NODE_MIRROR=https://mymirror.com/dist
# Set a mirror that has the same directory structure as https://nodejs.org/dist
# (you may also use `NODIST_IOJS_MIRROR` in the same fashion).
> set NODIST_X64=0
# (Set to `1` to enforce 64bit, `0` to enforce 32bit.)
> set NODIST_INSPECT_PACKAGEJSON=1
# Enable package.json inspection: Nodist will check the engines field of the package.json file of scripts to be executed and use the node version specified there. Turned off by default as of v0.8.5.
Node executables are stored in NODIST_PREFIX\v
and NODIST_PREFIX\v-x64
, npm versions in NODIST_PREFIX\npmv.
node.exe` is a shim and chooses the right node version to run based on the various version settings. The same applies for npm.
The global
As the global node version will be subject to change, nodist
comes with its own dedicated node binary.
File an issue and tell me what you’d change or add or what doesn’t work for you. Every issue is welcome!
To see all debug messages, set the DEBUG env var before running nodist or node as follows:
> set DEBUG=nodist:*
The default test suite can be ran using npm
$ npm test
Testing also accepts env variables for using a mirror to download from, as well as setting a proxy.
> set NODIST_NODE_MIRROR=http://nodejs.serverpals.com/dist
> vows --spec test\cli-test.js
Building nodist requires
* go for compiling the shim
* NSIS v3 for compiling the installer (use the large strings build to avoid errors when manipulating PATH)
* NSIS Plugin: AccessControl
* node.js for running the build script
* and npm for installing nodist’s dependencies
* Finally you need to go get github.com/marcelklehr/semver github.com/tj/go-debug
If you have met all requirements, run the build command:
> npm run build
Afterwards you’ll find the installer in build/out/NodistSetup.exe
and fully prepared installation folder in build/out/staging
(you could zip this, for example).
The chocolatey package will be in build/out/package
, you can run cpack
and cpush --source https://chocolatey.org/
inside that directory (if you are a registered maintainer).
Copyright (c) 2012-2016 by Marcel Klehr
MIT License
v0.8.6
* Fix installer: Increase MAX_LEN for manipulating PATH to 8000
* Add support for WebStorm and VisualStudio
v0.8.5
* Disable package.json inspection by default
v0.8.4
* Fix version spec parsing in shims
v0.8.3
* Fix npm builds: Lock node version for npm’s child processes
v0.8.2
* Fix Git bash support and add support for Cygwin
* Fix nodist dist
output being unsorted
v0.8.1
* Fix usage/help output: stable
has been removed
* Fix installer: Use the correct path to npm when setting npm prefix
v0.8.0
* Add NPM version management (thanks to @nullivex)
* Treat io.js versions as node versions
* Allow setting ranges in global/local/env (don’t resolve before setting versions)
* Drop support for setting node command line args
* Respect engines field declaration in package.json
* Fix local switching: Use the target script’s dir as the base dir
* Allow setting env vars for mirror support
* Support bash
* Remove selfupdate command
* Fix help flag
* [installer] Fix: Set system not user PATH
* [installer] Fix: auto-detect x64 arch
* Improve build script
* Revive chocolatey package
v0.7.2
* correct version of NPM
* Fixed offline support (#104)
v0.7.1
* Fix x64 support
v0.7.0 (thanks to @nullivex)
* Add support for hashing downloading binaries and comparing to upstream
* Code refactor to implement standards similar to npmjs style
* Implement download progress bar
* Add debugging output to aid in development and issue handling
* Bump NPM version to 3.3.8
* Improve testing and offer ability to see test output
* Testing now accepts mirror env variables
* Env variables can now be used to point nodist at a mirror
* Only downloads binaries from HTTPS now as node is phasing out HTTP support
v0.6.1
* Fix for node v4 dist directory structure (thanks to @jakub-g)
v0.6.0
* Write out version spec that’s being used, to make version errors more intuitive. (thanks to @blzaugg)
* Fix replace for nodistPrefix and envVersion (thanks to @sdovenor)
* Fix chocolatey package (thanks to @u9520107)
* Add support for io.js
v0.5.2
* Fix nodist version
v0.5.1
* Fix npm prefix in nodist selfupdate
v0.5.0
* Use a binary shim
* Support for localized version switching
v0.4.8
* Check in dependencies to avoid relying on npm on install
v0.4.7
* Update bundled npm to v1.4.6 and check-in all files
v0.4.6
* Update pre-packaged npm to v1.4.3
v0.4.5
* Fix nodist -v
in powershell (thanks to @devert for reporting)
v0.4.4
* Fix GOTO in batch file (thanks to @BergWerkGIS)
v0.4.3
* Fix a type coercion catch in x64 detection (NODIST_X64=0 had the same effect as NODIST_X64=1; thanks to @springmeyer for reporting)
v0.4.2
* Use the right URI for fetching the x64 executables
v0.4.1
* x64 auto-detection
v0.4.0
* Refactor 64bit support (All commands now work with x64 versions)
* Make npm use the user-chosen node version (thanks to @k-j-kleist for reporting)
* Fix some minor bugs in error handling
* Don’t try installing ‘vUpdate’ after running nodist update
* Install latest stable version after running nodist update
v0.3.11
v0.3.10
v0.3.9
nodist update
v0.3.8
v0.3.7
nodist run
and list
commandsv0.3.5
v0.3.4
v0.3.3
v0.3.2
v0.3.1
nodist + all
nodist update
must be run at installv0.3.0
optimist
(caused more problems than it solved)node-semver
, now, for better usability… (fixes #10)v0.2.8
v0.2.7
v0.2.6
v0.2.5
v0.2.4
nodist update
command to work from all drivesnpm u npm -g
v0.2.3
总结
以上就是两种Node版本管理工具的安装和基本使用方法,选择适合你的那一种口味。
参考
https://github.com/creationix/nvm
https://github.com/visionmedia/n
https://github.com/NanJingBoy/nvmw
https://github.com/marcelklehr/nodist