之前一直使用的是node v2.2.0版本,挺说新版本的node解决了npm安装插件产生文件夹结构过深的问题,所以就想更新试试;
上网一看才发现,尼玛的node已经到了6.+版本了,好吧,看来还是得跟上节奏;
但是呢,怎么更新node就成了个问题,卸载重装? 这样又会出现一些配置的麻烦;
经过一番查找发现:可以使用 nvm 管理不同版本的 node 与 npm;
nvm 是 Mac 下的 node 管理工具,有点类似管理 Ruby 的 rvm,如果是需要管理 Windows 下的 node,官方推荐是使用 nvmw 或 nvm-windows 。
接下来就记录一下我安装nvmw的具体过程和一些坑;(不要问我为什么选nvmw,因为穷叼不会用Mac,而-windows貌似有些问题)
百度了一早上,终于完成了node的升级,从头开始来吧,也整理了一下其中遇到的问题;
1、首先下载 nvmw https://github.com/hakobera/nvmw
方法:
git clone https://github.com/hakobera/nvmw.git;或者直接下载zip压缩包;
由于本人的git不知道抽什么疯,clone的时候一直出现
如果有大神知道是什么问题,请评论留言;万分感谢;
2、配置环境变量
复制nvmw路径,我的电脑右键→属性→高级系统设置→高级→环境变量
这里我用户环境和系统环境都该了,照理说改系统环境就可以了;
这里有个坑,就是修改变量后要重新关闭CMD,重开一次,才能正常运用;
3、配置完后打开CMD,输入nvmw,如果正确配置的话可以查看帮助
百度的时候听说 nvmw install v6.9.2 无法安装成功;
我没试过,不过我用的是没有v的 nvmw install 6.9.2成功了;
4、不要高兴的太早,这才是刚刚开始;
第一个坑: 直接使用nvmw install 6.9.2 来安装新版本时提示:
解决办法:这样的错误,原因是因为JS扩展名的文件被其他软件关联了,需要取消关联。
如系统中安装了ULTRAEDIT或者E钻加密软件等,就需要去掉关联;
或者如下解决方法:
在运行中输入“regedit”进入注册表,
只需要把[HKEY_CLASSES_ROOT\.js] 项下的那个默认值改成 "JSFile" 就可以正常运行JS 文件了。
第二个坑:
这货直接在天朝是使用不了的,好在这个外国作者也知道中国的国情。提供了如何设置淘宝镜像的方式
set "NVMW_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node"
set "NVMW_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs"
set "NVMW_NPM_MIRROR=http://npm.taobao.org/mirrors/npm"
注:每次使用,都得重新在命令行中设置,或者可以这三个变量也设置到环境变量中,或改一下他的源码,把这三个变量写死。
第三个坑:我是windows7系统,对应的是x64没错,但是在淘宝NPM镜像站和官方镜像站上对应的都是win-x64 ;
进入nvmw的程序目录。
1.修改nvmw.bat文件137行,x64改为win-x64
2.修改fget.js文件47行,XMLHTTP 更改为ServerXMLHTTP
其实有现成的修改版的nvmw.bat 一下就是代码,可以留个副本;
@echo off
if not defined NVMW_HOME (
set "NVMW_HOME=%~dp0"
)
if not defined PATH_ORG (
set "PATH_ORG=%PATH%"
)
set IS64=FALSE
if exist "%PROGRAMFILES(X86)%" (
set IS64=TRUE
)
if %IS64% == TRUE (
set OS_ARCH=x64
) else (
set OS_ARCH=x32
)
if not defined NVMW_NODEJS_ORG_MIRROR (
set "NVMW_NODEJS_ORG_MIRROR=https://nodejs.org/dist"
)
if not defined NVMW_IOJS_ORG_MIRROR (
set "NVMW_IOJS_ORG_MIRROR=https://iojs.org/dist"
)
if "%1" == "install" if not "%2" == "" (
call :install %2 %3
if not ERRORLEVEL == 1 call :use %2 %3
exit /b %ERRORLEVEL%
)
if "%1" == "use" if not "%2" == "" (
call :use %2 %3
exit /b %ERRORLEVEL%
)
if "%1" == "ls" (
call :ls
exit /b %ERRORLEVEL%
)
if "%1" == "uninstall" if not "%2" == "" (
call :uninstall %2 %3
exit /b %ERRORLEVEL%
)
call :help
exit /b %ERRORLEVEL%
::===========================================================
:: help : Show help message
::===========================================================
:help
echo;
echo Node Version Manager for Windows
echo;
echo Usage:
echo nvmw help Show this message
echo nvmw install [version] [arch] Download and install a [version]
echo for [arch] architecture (optional)
echo nvmw uninstall [version] Uninstall a [version]
echo nvmw use [version] Modify PATH to use [version]
echo nvmw ls List installed versions
echo;
echo Example:
echo nvmw install v0.10.21 Install a specific version number of node.js
echo nvmw use v0.10.21 Use the specific version
echo nvmw install iojs Install the latest version of io.js
echo nvmw install iojs-v1.0.2 Install a specific version number of io.js
echo nvmw use iojs-v1.0.2 Use the specific version io.js
echo;
echo nvmw install v0.10.35 x86 Install a 32-bit version
exit /b 0
::===========================================================
:: install : Install specified version node and npm
::===========================================================
:install
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
:: nvmw install iojs-v1.0.2
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw install iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw install node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
:: iojs-1.0.0, iojs-latest
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
if %NODE_TYPE% == iojs (
set DIST_URL=%%
if %ARCH% == x32 (
set NODE_EXE_URL=%NVMW_IOJS_ORG_MIRROR%/%NODE_VERSION%/win-x86/iojs.exe
) else (
set NODE_EXE_URL=%NVMW_IOJS_ORG_MIRROR%/%NODE_VERSION%/win-x64/iojs.exe
)
) else if %NODE_VERSION:~0,1% == 0 (
if %ARCH% == x32 (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/node.exe
) else (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/x64/node.exe
)
) else (
if %ARCH% == x32 (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/win-x86/node.exe
) else (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/win-x64/node.exe
)
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
set "NODE_EXE_FILE=%NODE_HOME%\%NODE_TYPE%.exe"
set "NPM_ZIP_FILE=%NODE_HOME%\npm.zip"
if exist "%NODE_EXE_FILE%" (
endlocal
echo "%NODE_TYPE%/%NODE_VERSION% (%ARCH%)" already exists, please uninstall it first
exit /b 1
)
mkdir "%NODE_HOME%"
echo Start installing %NODE_TYPE%/%NODE_VERSION% (%ARCH%) to %NODE_HOME%
cscript //nologo "%NVMW_HOME%\fget.js" %NODE_EXE_URL% "%NODE_EXE_FILE%"
if not exist "%NODE_EXE_FILE%" (
echo Download %NODE_EXE_FILE% from %NODE_EXE_URL% failed
goto install_error
) else (
if %NODE_TYPE% == iojs (
copy "%NVMW_HOME%\alias-node.cmd" "%NODE_HOME%\node.cmd"
)
echo Start install npm
"%NODE_EXE_FILE%" "%NVMW_HOME%\get_npm.js" "%NODE_HOME%" "%NODE_TYPE%/%NODE_VERSION%"
if not exist "%NPM_ZIP_FILE%" goto install_error
set "CD_ORG=%CD%"
%~d0
cd "%NODE_HOME%"
echo Start unzip "%NPM_ZIP_FILE%" to "%NODE_HOME%"
cscript //nologo "%NVMW_HOME%\unzip.js" "%NPM_ZIP_FILE%" "%NODE_HOME%"
mkdir "%NODE_HOME%\node_modules"
rmdir /s /q "%NODE_HOME%\node_modules\npm"
move npm-* "%NODE_HOME%\node_modules\npm"
copy "%NODE_HOME%\node_modules\npm\bin\npm.cmd" "%NODE_HOME%\npm.cmd"
cd "%CD_ORG%"
if not exist "%NODE_HOME%\npm.cmd" goto install_error
echo npm install ok
echo Finished
endlocal
exit /b 0
)
:install_error
rd /Q /S "%NODE_HOME%"
endlocal
exit /b 1
::===========================================================
:: uninstall : Uninstall specified version
::===========================================================
:uninstall
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw uninstall iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw uninstall node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
if "%NVMW_CURRENT_TYPE%" == "%NODE_TYPE%" if "%NVMW_CURRENT%" == "%NODE_VERSION%" if "%NVMW_CURRENT_ARCH%" == "%ARCH%" (
echo Cannot uninstall currently-active %NODE_TYPE%/%NODE_VERSION% %ARCH%
exit /b 1
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
if not exist "%NODE_HOME%" (
echo %NODE_TYPE%/%NODE_VERSION% %ARCH% is not installed
exit /b 1
) else (
rd /Q /S "%NODE_HOME%"
if ERRORLEVEL == 1 (
echo Cannot uninstall %NODE_TYPE%/%NODE_VERSION% %ARCH%
exit /b 1
) else (
echo Uninstalled %NODE_TYPE%/%NODE_VERSION% %ARCH%
endlocal
exit /b 0
)
)
::===========================================================
:: use : Change current version
::===========================================================
:use
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw use iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw use node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
if not exist "%NODE_HOME%" (
echo %NODE_TYPE%/%NODE_VERSION% is not installed
exit /b 1
)
endlocal
set NVMW_CURRENT_ARCH=%OS_ARCH%
if not "%2" == "" (
set NVMW_CURRENT_ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set NVMW_CURRENT_ARCH=x32
)
if "%2" == "ia32" (
set NVMW_CURRENT_ARCH=x32
)
)
set NVMW_CURRENT_TYPE=node
set NVMW_CURRENT=%1
if "%NVMW_CURRENT:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NVMW_CURRENT%") do (
set NVMW_CURRENT_TYPE=%%a
set NVMW_CURRENT=%%b
)
)
if %NVMW_CURRENT% == iojs (
set NVMW_CURRENT_TYPE=iojs
set NVMW_CURRENT=latest
)
if %NVMW_CURRENT% == node (
set NVMW_CURRENT_TYPE=node
set NVMW_CURRENT=latest
)
if not %NVMW_CURRENT:~0,1% == v if not %NVMW_CURRENT:~0,1% == l (
set NVMW_CURRENT=v%NVMW_CURRENT%
)
echo Now using %NVMW_CURRENT_TYPE% %NVMW_CURRENT% %NVMW_CURRENT_ARCH%
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NVMW_CURRENT_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %NVMW_CURRENT_ARCH% == %OS_ARCH% (
set NVMW_CURRENT_ARCH_PADDING=-%NVMW_CURRENT_ARCH%
) else (
set NVMW_CURRENT_ARCH_PADDING=
)
if %NVMW_CURRENT_TYPE% == iojs (
set "PATH=%NVMW_HOME%;%NVMW_HOME%%NVMW_CURRENT_TYPE%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%;%PATH_ORG%"
set "NODE_PATH=%NVMW_HOME%%NVMW_CURRENT_TYPE%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%\node_modules"
) else (
set "PATH=%NVMW_HOME%;%NVMW_HOME%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%;%PATH_ORG%"
set "NODE_PATH=%NVMW_HOME%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%\node_modules"
)
exit /b 0
::===========================================================
:: ls : List installed versions
::===========================================================
:ls
setlocal
echo node:
if exist "%NVMW_HOME%" (
dir "%NVMW_HOME%v*" /b /ad
)
echo;
echo iojs:
if exist "%NVMW_HOME%iojs" (
dir "%NVMW_HOME%iojs\*" /b /ad
)
echo;
if not defined NVMW_CURRENT (
set NVMW_CURRENT_V=none
) else (
set NVMW_CURRENT_V=%NVMW_CURRENT%
)
echo Current: %NVMW_CURRENT_TYPE%/%NVMW_CURRENT_V% %NVMW_CURRENT_ARCH%
endlocal
exit /b 0
第四个坑:更新node到4.0及以上的时候会出现安装失败 提示 node v6.9.2 does not include npm;
办法: https://github.com/hakobera/nvmw/issues/58
https://my.oschina.net/bakuman/blog/637316
http://blog.5ibc.net/p/49266.html
按文章内容修改了nvmw.bat,然后手动下载npm-master.zip并改名为npm.zip后放到nvmw目录下新建的v4.4.0目录下,然后重新的执行 nvmw install 4.4.0,终于安装成功。(CMD执行 nvmw install 6.9.2的时候会在nvmw安装目录中生产一个对应版本的文件夹,这时候把npm.zip压缩包放进去,就能完成升级);
这样我们就可以控制nodejs的版本了,我的天,真心的麻烦~~
最后一步,我们可以使用 npm install npm -g 来获得新版的npm了;
总算大功告成,大多数的解决办法都是从网上找到的;
2017.02.22补充说明;
使用nvmw install 版本号 安装了之后,并不会马上转变成安装的版本,
需要使用nvmw use v6.9.2 (版本号看个人需求)进行版本的转换,install只是保存到本地供版本替换使用;
下面是参考链接:
http://www.tuicool.com/articles/Vzquy2
http://blog.****.net/duanyachao/article/details/51822015
http://www.wtoutiao.com/p/193HWBp.html
https://segmentfault.com/a/1190000004855835
https://my.oschina.net/bakuman/blog/637316
http://blog.5ibc.net/p/49266.html
https://github.com/hakobera/nvmw/issues/58
好了,希望能帮到需要的同学们。
2017.01.06
2018.10.11
换win10的环境后安装没有这么多的麻烦了,具体参考:https://segmentfault.com/a/1190000004855835
其中第三点.修改get_npm.js文件42行路径,其实为
使用nvmw install 5.9.1安装特定版本node时,最后会提示D:\nvmw\v5.9.1\node.exe - 另一个程序正在使用此文件,进程无法访问。
但是实质上是安装成功的
nvmw use 5.9.1
node -v
可以查看node版本已经切换为特定版本了