Windows 下 nodejs 安装
Node.js安装包及源码下载地址为:https://nodejs.org/en/download/。
1、Windows 安装包(.msi)
下载node.msi, https://nodejs.org/en/download/
执行该安装程序,按提示一直到最后完成安装。
默认的缓存目录为: %APPDATA%\npm-cache
默认的全局安装目录为: %APPDATA%\npm-cache
2、Windows 二进制文件 (.exe)安装
- 下载node.exe, https://nodejs.org/en/download/
-
创建安装目录,本例为
c:\nodejs
并将node.exe拷贝至该目录中, -
把
c:\nodejs
加入path环境变量中,可以打开命令行使用node -v
测试。 -
下载 npm源码 , https://github.com/npm/npm/tags
-
解压npm源码包至任意位置,本例中为
d:\software\nodejs\npm-3.10.6
,命令行下进入该目录并执行node cli.js install npm -gf
进行安装。1234567891011121314151617181920212223d:\software\nodejs\npm-3.10.6>node cli.js install -gfnpm WARN using --force I sure hope you know what you are doing.> npm@3.10.6 prepublish d:\software\nodejs\npm-3.10.6> node bin/npm-cli.js prune --prefix=. --no-global && rimraf test/*/*/node_modules && make doc-clean && make -j4 docnpm WARN using --force I sure hope you know what you are doing.npm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: ansi-regexnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: debuglognpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: imurmurhashnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash._baseindexofnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash._bindcallbacknpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash._cacheindexofnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash._createcachenpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash._getnativenpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: lodash.restparamnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: readdir-scoped-modulesnpm WARN package.json npm@3.10.6 Non-dependency in bundleDependencies: validate-npm-package-license'rimraf' 不是内部或外部命令,也不是可运行的程序或批处理文件。npm WARN Local package.json exists, but node_modules missing, did you mean to install?C:\nodejs\npm -> C:\nodejs\node_modules\npm\bin\npm-cli.js以上为命令执行后的输出信息,除了一些警告外,有一条报错信息:
123'rimraf' 不是内部或外部命令,也不是可运行的程序或批处理文件。由输出信息第4行可以看出,在安装过程中执行了
rimraf test/*/*/node_modules
命令,而rimraf
是一个递归删除文件的nodejs包,作用同 Linux 下的rm -rf
,我们此时并没有安装它,所以报错了。其实看这条命令就发现这个报错并不影响什么,如果我们在安装了rimraf
的情况下删除重装 npm ,是不会报错的。注意,这一步会将 npm 安装到 node.exe 所在目录下的
.\node_modules\npm
中,在本例中为:C:\nodejs\node_modules\npm
,因此不能把npm源码解压至该目录。 -
设置npm全局安装目录和缓存目录
安装成功后,默认的缓存目录为:
%APPDATA%\npm-cache
,默认的全局安装目录为node.exe 所在目录。1234567#查看npm config get prefixnpm config get cache#设置npm config set prefix "D:\node\node-global"npm config set cache "D:\node\node-cache"
3、npm单独安装
有时候我们需要更换 npm 的版本或重新安装,这时有两种办法。
- 替换文件
可以使用
npm install npm@{version}
安装指定版本的npm到局部目录,若使用npm install npm
则安装最新版。完成后将局部目录下新安装的node_modules\npm
文件夹覆盖至当前 npm 所在目录。完成后通过npm -v
查看版本。 -
删除重装
先到 github 上下载相应版本的 npm 源码包,地址:https://github.com/npm/npm/tags
删除 nodejs 安装目录中的npm相关的脚本等文件,删除node_modules下的 npm 文件夹
参照本文中 Windows 二进制文件 (.exe)安装 条目下第5步完成重装。