nodejs 环境变量配置。

  • Post author:
  • Post category:其他


  1. 官网下载二进制解压包

  2. 将解压后的路径配置到系统变量中,打开cmd,以管理员运行,输入node -v,npm -v 。检测nodejs是否生效。

  3. 配置全局变量

    在安装目录下【D:\Nodejs\node】创建两个文件夹【node_global】及【node_cache】用来配置全局环境变量。


    npm config set prefix "D:\nodejs\node\node_global" npm config set cache "D:\nodejs\node\node_cache"

  4. 配置nodejs全局环境变量

    在【用户变量】的path下,加入全局环境变量的路径:D:\node\node_global;


    全局环境变量的路径必须写在最前面。


    在【系统环境变量】path下配置nodeJS安装路径:D:\nodejs\node\node_global;


gulp-v 命令不生效解决办法。


– 全局安装gulp命令:


全局 npm install -g gulp

项目里 npm install gulp –save-dev

npm install


– 安装插件

npm install --save-dev gulp-uglify


– nodejs 换源



第一种



直接设置淘宝的源:npm config set registry http://registry.npm.taobao.org/

然后使用npm install xxx

可用


第二种

:安装cnpm,npm install -g cnpm –registry=https://registry.npm.taobao.org

然后以后都是用cnpm安装插件,cnpm install xxx


第三种

:使用nrm

1.下载nrm:npm install -g nrm

2.列出可以选择的源:nrm ls

3.选择想要使用的源:nrm use taobao

4.查看当前npm使用的源:npm config get registry

5.后续的想安装什么插件直接使用npm install xxx就行了

get命令查看registry

npm config get registry

可用 get命令查看registry

http://registry.npmjs.org

用set命令换成阿里的镜像

npm config set registry https://registry.npm.taobao.org

或者直接执行

npm install --registry=https://registry.npm.taobao.org
npm update



npm获取配置有6种方式,优先级由高到底。

  1. 命令行参数。 –proxy http://server:port即将proxy的值设为http://server:port。
  2. 环境变量。 以npm_config_为前缀的环境变量将会被认为是npm的配置属性。如设置proxy可以加入这样的环境变量npm_config_proxy=http://server:port。
  3. 用户配置文件。可以通过npm config get userconfig查看文件路径。如果是mac系统的话默认路径就是$HOME/.npmrc。
  4. 全局配置文件。可以通过npm config get globalconfig查看文件路径。mac系统的默认路径是/usr/local/etc/npmrc。
  5. 内置配置文件。安装npm的目录下的npmrc文件。
  6. 默认配置。 npm本身有默认配置参数,如果以上5条都没设置,则npm会使用默认配置参数。

如果要查看npm的所有配置属性(包括默认配置),可以使用npm config ls -l

如果要查看npm的各种配置的含义,可以使用npm help config


为npm设置代理

npm config set proxy http://server:port
npm config set https-proxy http://server:port

如果代理需要认证的话可以这样来设置。

npm config set proxy http://username:password@server:port
npm config set https-proxy http://username:pawword@server:port

如果代理不支持https的话需要修改npm存放package的网站地址。

npm config set registry "http://registry.npmjs.org/"



npm proxy 设定


设置代理

npm config set proxy=http://127.0.0.1:8087
npm config set registry=http://registry.npmjs.org


关于https


经过上面设置使用了http开头的源,因此不需要设https_proxy了,否则还要增加一句:

npm config set https-proxy http://server:port


代理用户名和密码

npm config set proxy http://username:password@server:port
npm confit set https-proxy http://username:password@server:port


取消代理

npm config delete proxy
npm config delete https-proxy



npm安装依赖包出错问题处理

  1. 是否能够访问安装源:检查代理和当前安装源
  2. 当前的node版本是否兼容已有模块node_modules
  3. 输入的依赖包名称是否正确


出现npm ERR! code ECONNRESET错误


在这里插入图片描述

这些错误的原因很有可能是npm使用默认的源下载安装包,而默认的安装源是国外网站,国内访问不了无法获取依赖包信息。

这时只需要更换为国内的安装源即可,可在命令行更换为国内淘宝的源:

# 查看自己的安装源
npm config get registry

# 更换npm源为国内淘宝镜像
npm config set registry http://registry.npm.taobao.org/

# 或者更换为国内npm官方镜像
npm config set registry http://registry.cnpmjs.org/

# 还原npm源
npm config set registry https://registry.npmjs.org/


npm安装源设置


上面介绍了通过命令行设置安装源地址的办法,另外还可以设置代理,以及编辑配置文件等方法。

# 命令行设置代理
npm config set proxy="http://127.0.0.1:1034"

# 还可以直接使用npm设置,不需要config
npm --registry https://registry.npm.taobao.org info underscore

另外还可以直接编辑npm安装配置文件:

.npmrc

,在文件末尾添加两行:

registry="http://registry.npmjs.org"

 proxy="http://127.0.0.1:1034"

这个文件一般在用户目录或者安装目录下。



SSL Error: CERT_UNTRUSTED

这个是因为HTTPS的设置问题,可以有两种办法解决:

  1. 关掉SSL检测
  2. 使用HTTP连接的安装源
# 关闭SSL检查
npm config set strict-ssl false

# 使用http安装源
npm config set registry="http://registry.npmjs.org/"



npm WARN unmet dependency

这个错误的原因很有可能是按照依赖包的过程重网络超时等导致,可以通过清空node_module修复:

# 删除node_modules目录
rm -rf node_modules/

# 情况缓存
npm cache clean

# 重新安装
npm install

有时候,因为node版本过低也会导致该问题,可以使用下面命令更新node:

npm update -g npm



npm ERR! code EINTEGRITY

npm install时报错:

npm ERR! code EINTEGRITY

npm ERR! sha512- sha512-rkIa1OSVWTt4g9leLSK/PsqOj3HZbDKHbZj

这个问题有可能是npm版本过低导致,需要更新npm版本:

# 更新npm
npm install -g npm

# 继续安装
npm install



npm ERR! code EPERM

安装时出现:

npm ERR! code EPERM

npm ERR! errno -4048

这个错误出现的原因很多,其中一个是因为npm安装缓存的问题,可以尝试执行下面命令清空缓存。

npm cache clean --force



版权声明:本文为qq_42603181原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。