有没有一种方法可以使npm install(命令)在代理后面工作?

node.js Node.js

L西里

2020-03-20

了解.npmrc文件中的代理变量,但是它不起作用。尝试避免手动下载所有必需的软件包并进行安装。

第2444篇《有没有一种方法可以使npm install(命令)在代理后面工作?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

21个回答
Tony西里 2020.03.20

Just open the new terminal and type npm config edit and npm config -g edit. Reset to defaults. After that close terminal, open the new one and type npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package> if you need globally just add -g.

It worked for me, hope it`ll work for you :)

乐Jim 2020.03.20

when I give without http/http prefix in the proxy settings npm failed even when the proxy host and port were right values. It worked only after adding the protocol prefix.

神无斯丁 2020.03.20

In my case, I forgot to set the "http://" in my config files (can be found in C: \Users \ [USERNAME] \ .npmrc) proxy adresses. So instead of having

proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]

I had

proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]

Which of course did not work, but the error messages didnt help much either...

飞云村村 2020.03.20

A lot of applications (e.g. npm) can use proxy setting from user environment variables.

You can just add to your environment following variables HTTP_PROXY and HTTPS_PROXY that will have the same value for each one

http://user:password@proxyAddress:proxyPort

For example if you have Windows you can add proxy as follow:

How it looks on Windows

Stafan小宇宙 2020.03.20

Use below command at cmd or GIT Bash or other prompt

$ npm config set proxy "http://192.168.1.101:4128"

$ npm config set https-proxy "http://192.168.1.101:4128"

where 192.168.1.101 is proxy ip and 4128 is port. change according to your proxy settings. its works for me.

LEY乐猴子 2020.03.20

For me even though python etc will all work though our corporate proxy npm would not.

I tried

npm config set proxy http://proxyccc.xxx.ca:8080 npm config set https-proxy https://proxyccc.xxx.ca:8080 npm config set registry http://registry.npmjs.org/

as instructed but kept getting the same error.

It was only when I removed https-proxy https://proxyccc.xxx.ca:8080 from the .npmrc file that npm install electron --save-dev worked

神乐Tony阿飞 2020.03.20

Try to find .npmrc in C:\Users\.npmrc

then open (notepad), write, and save inside :

proxy=http://<username>:<pass>@<proxyhost>:<port>

PS : remove "<" and ">" please !!

梅猪猪 2020.03.20

This worked for me. Set the http and https proxy.

逆天理查德 2020.03.20
npm config set proxy <http://...>:<port_number>
npm config set registry http://registry.npmjs.org/

This solved my problem.

西里阿飞 2020.03.20

Though i set proxy with config, problem was not solved but after This one worked for me:

npm --https-proxy http://XX.AA.AA.BB:8080 install cordova-plugins

npm --proxy http://XX.AA.AA.BB:8080 install

Green小卤蛋 2020.03.20

Finally i have managed to solve this problem being behinde proxy with AD authentication. I had to execute:

npm config set proxy http://domain%5Cuser:password@proxy:port/
npm config set https-proxy http://domain%5Cuser:password@proxy:port/

It is very important to URL encode any special chars like backshlash or # In my case i had to encode

  1. backshlash with %5C so domain\user will be domain%5Cuser
  2. # sign with %23%0A so password like Password#2 will be Password%23%0A2

I have also added following settings:

npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/
神奇猴子米亚 2020.03.20

vim ~/.npmrc在您的Linux机器中并添加以下内容。不要忘记添加registry零件,因为在很多情况下这会导致故障。

proxy=http://<proxy-url>:<port>
https-proxy=https://<proxy-url>:<port>
registry=http://registry.npmjs.org/
Mandy前端斯丁 2020.03.20

这对我有用-

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm set strict-ssl=false
TonyEvaL 2020.03.20
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
Itachi猪猪 2020.03.20

这在Windows中对我有效:

npm config set proxy http://domain%5Cuser:pass@host:port

如果您不在任何域中,请使用:

npm config set proxy http://user:pass@host:port

如果您的密码包含特殊字符,例如"@:等等,通过他们的URL编码值替换它们。例如"-> %22@-> %40:-> %3A%5C用于角色\

泡芙Stafan 2020.03.20

要设置http代理,请设置-g标志:

sudo npm config set proxy http://proxy_host:port -g

对于https代理,再次确保设置-g标志:

sudo npm config set https-proxy http://proxy_host:port -g

小胖Sam 2020.03.20

尽管已经有了很多好的建议,但对于我的环境(Windows 7,使用PowerShell)和最新可用的node.js版本(v8.1.2),上述所有方法均无效,除非我遵循brunowego设置。

因此,请使用以下命令检查设置:

npm config list

代理背后的设置:

npm config set registry http://registry.npmjs.org/
npm config set http-proxy http://username:password@ip:port
npm config set https-proxy http://username:password@ip:port
npm config set proxy http://username:password@ip:port
npm set strict-ssl false

希望这可以节省时间给某人

猪猪乐理查德 2020.03.20

您是否尝试过命令行选项而不是.npmrc文件?

我认为有些事情npm --proxy http://proxy-server:8080/ install {package-name}对我有用。

我还看到了以下内容: npm config set proxy http://proxy-server:8080/

阿飞村村 2020.03.20

如有疑问,请像我一样尝试所有这些命令:

npm config set registry http://registry.npmjs.org/
npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
set HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
set HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export http_proxy=http://myusername:mypassword@proxy.us.somecompany:8080

npm --proxy http://myusername:mypassword@proxy.us.somecompany:8080 \
--without-ssl --insecure -g install

=======

更新

将设置放进去~/.bashrc~/.bash_profile这样您就不必在每次打开新的终端窗口时都担心设置!

If your company is like mine, I have to change my password pretty often. So I added the following into my ~/.bashrc or ~/.bash_profile so that whenever I open a terminal, I know my npm is up to date!

  1. Simply paste the following code at the bottom of your ~/.bashrc file:

    ######################
    # User Variables (Edit These!)
    ######################
    username="myusername"
    password="mypassword"
    proxy="mycompany:8080"
    
    ######################
    # Environement Variables
    # (npm does use these variables, and they are vital to lots of applications)
    ######################
    export HTTPS_PROXY="http://$username:$password@$proxy"
    export HTTP_PROXY="http://$username:$password@$proxy"
    export http_proxy="http://$username:$password@$proxy"
    export https_proxy="http://$username:$password@$proxy"
    export all_proxy="http://$username:$password@$proxy"
    export ftp_proxy="http://$username:$password@$proxy"
    export dns_proxy="http://$username:$password@$proxy"
    export rsync_proxy="http://$username:$password@$proxy"
    export no_proxy="127.0.0.10/8, localhost, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16"
    
    ######################
    # npm Settings
    ######################
    npm config set registry http://registry.npmjs.org/
    npm config set proxy "http://$username:$password@$proxy"
    npm config set https-proxy "http://$username:$password@$proxy"
    npm config set strict-ssl false
    echo "registry=http://registry.npmjs.org/" > ~/.npmrc
    echo "proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "strict-ssl=false" >> ~/.npmrc
    echo "http-proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "http_proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "https_proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "https-proxy=http://$username:$password@$proxy" >> ~/.npmrc
    
    ######################
    # WGET SETTINGS
    # (Bonus Settings! Not required for npm to work, but needed for lots of other programs)
    ######################
    echo "https_proxy = http://$username:$password@$proxy/" > ~/.wgetrc
    echo "http_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc
    echo "ftp_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc
    echo "use_proxy = on" >> ~/.wgetrc
    
    ######################
    # CURL SETTINGS
    # (Bonus Settings! Not required for npm to work, but needed for lots of other programs)
    ######################
    echo "proxy=http://$username:$password@$proxy" > ~/.curlrc
    
  2. Then edit the "username", "password", and "proxy" fields in the code you pasted.

  3. Open a new terminal

  4. Check your settings by running npm config list and cat ~/.npmrc

  5. Try to install your module using

    • npm install __, or
    • npm --without-ssl --insecure install __, or
    • override your proxy settings by using npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install __.
    • If you want the module to be available globally, add option -g
LEY米亚 2020.03.20

设定npm代理

对于HTTP

npm config set proxy http://proxy_host:port

对于HTTPS

如果有一个,请使用https代理地址

npm config set https-proxy https://proxy.company.com:8080

否则重用http代理地址

npm config set https-proxy http://proxy.company.com:8080

注意:https-proxy没有https作为协议,而是http

西里米亚 2020.03.20

我这样解决了这个问题:

  1. 我运行以下命令:

    npm config set strict-ssl false
    
  2. 然后将npm设置为使用http而不是https运行:

    npm config set registry "http://registry.npmjs.org/"
    
  3. 然后,我使用以下语法安装软件包:

    npm --proxy http://username:password@cacheaddress.com.br:80 install packagename
    

username:password如果代理不需要您进行身份验证,请跳过该部分

编辑:我的一个朋友刚刚指出的是,你可以通过设置让NPM背后的代理工作BOTH HTTP_PROXY和HTTPS_PROXY环境变量,然后正常发出命令 故宫安装快车(例如)

EDIT2:正如@BStruthers所评论的那样,请记住,如果包含@,则不能正确地解析包含“ @”的密码,如果将@整个密码放在引号中

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android