Node.js global proxy setting - node.js

I was working in a corporate network behind a proxy server. In my code I can set the proxy by using the approach mentioned in this thread.
But the problem is that most of the 3rd party modules do not have proxy setting and I cannot modify their code to add the proxy. Also, my code might be used in a direct connection environment which means I cannot hard-code my proxy setting in code.
I know NPM has a global setting for proxy which is
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
But I didn't find any config similar in Node.js.
Does Node.js support global proxy setting so that I don't need to change all codes and switch on and off easily?

Unfortunately, it seems that proxy information must be set on each call to http.request. Node does not include a mechanism for global proxy settings.
The global-tunnel-ng module on NPM appears to handle this, however:
var globalTunnel = require('global-tunnel-ng');
globalTunnel.initialize({
host: '10.0.0.10',
port: 8080,
proxyAuth: 'userId:password', // optional authentication
sockets: 50 // optional pool size for each http and https
});
After the global settings are establish with a call to initialize, both http.request and the request library will use the proxy information.
The module can also use the http_proxy environment variable:
process.env.http_proxy = 'http://proxy.example.com:3129';
globalTunnel.initialize();

I finally created a module to get this question (partially) resolved. Basically this module rewrites http.request function, added the proxy setting then fire. Check my blog post: https://web.archive.org/web/20160110023732/http://blog.shaunxu.me:80/archive/2013/09/05/semi-global-proxy-setting-for-node.js.aspx

You can try my package node-global-proxy which work with all node versions and most of http-client (axios, got, superagent, request etc.)
after install by
npm install node-global-proxy --save
a global proxy can start by
const proxy = require("node-global-proxy").default;
proxy.setConfig({
http: "http://localhost:1080",
https: "https://localhost:1080",
});
proxy.start();
/** Proxy working now! */
More information available here: https://github.com/wwwzbwcom/node-global-proxy

While not a Nodejs setting, I suggest you use proxychains which I find rather convenient. It is probably available in your package manager.
After setting the proxy in the config file (/etc/proxychains.conf for me), you can run proxychains npm start or proxychains4 npm start (i.e. proxychains [command_to_proxy_transparently]) and all your requests will be proxied automatically.
Config settings for me:
These are the minimal settings you will have to append
## Exclude all localhost connections (dbs and stuff)
localnet 0.0.0.0/0.0.0.0
## Set the proxy type, ip and port here
http 10.4.20.103 8080
(You can get the ip of the proxy by using nslookup [proxyurl])

replace {userid} and {password} with your id and password in your organization or login to your machine.
npm config set proxy http://{userid}:{password}#proxyip:8080/
npm config set https-proxy http://{userid}:{password}#proxyip:8080/
npm config set http-proxy http://{userid}:{password}#proxyip:8080/
strict-ssl=false

Related

Setting up proxy from an address in nodejs

Suppose one is on a network address and these are the credentials:
Address: 172.a.b.c
Port:8080
Password:abcd
Am trying to set up node js where one is required to set up proxy:
npm config set proxy http://user:password#url:port
npm config set https-proxy http://user:password#url:port
Can someone help me in how will i add my address and password to the
npm config set....
I'm not 100% sure what you're asking? You're are asking how you attach your user + password to the npm configuration right? If so, this should help you:
npm config set proxy http://YOUR_USERNAME_HERE:YOUR_PASSWORD_HERE#url:YOUR_PORT_HERE
npm config set proxy-https http://YOUR_USERNAME_HERE:YOUR_PASSWORD_HERE#url:YOUR_PORT_HERE
It may end up looking like this
npm config set proxy http://james:jamesIsCool#url:3000

How to setup Node.js behind a corporate proxy

I have installed node.js in my windows machine which is in a corporate network. So i will have to use my Id and password to access internet through the proxy server.
I have read that we can use npm config set proxy to set the proxy.
npm config set proxy http://ABC\\123456:password#proxy.ABC.com:6050
I have tried it and is not working.
How can i specify the proxy details including username and password in NPM??
My user name is domain\username and password has special characters '!' and '#'
First open a command console at the location of your npm installation.
Then you can configure your npm to use a proxy using the commands:
npm config set proxy http://{url}:{port}
npm config set https-proxy http://{url}:{port}
Notice the protocol is set to http for both the proxy and https-proxy variables.
If you would like npm to store your credentials for the proxy,
you can additionally modify the commands as follows:
npm config set proxy http://{username}:{passphrase}#{url}:{port}
npm config set https-proxy http://{username}:{passphrase}#{url}:{port}
For example:
npm config set proxy http://LanguidSquid:Password1#my.company.com:8080
npm config set https-proxy http://LanguidSquid:Password1#my.company.com:8080
Additional information here: Using npm behind corporate proxy .pac
It's simple:
npm config set proxy http://username:password#proxy.company.com:8080
EDIT: Sorry didn't read about special chars:
You have to encode the special characters. E.g. instead of this:
http://username:p#ssword#proxy.company.com:8080
you have to write this:
http://username:p%40ssword#proxy.company.com:8080
Open an command prompt or terminal session and run the following commands to configure npm to work with your web proxy. The commands use proxy.company.com as the address and 8080 as the port.
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

npm Fails to Install over Socks5 Proxy

I am having trouble running npm install over a socks5 proxy.
Proxy is running locally, to tunnel http(s) over ssh to company intranet: ssh -vvv -D 5000 -N <SERVER_IP>
I have configured npm's proxy:
npm config set proxy socks5h://localhost:5000
npm config set https-proxy=socks5h://localhost:5000
I just get:
npm ERR! git clone https://<INTRANET>/repo fatal: unable to access 'https://<INTRANET>/repo/': Could not resolve host: <INTRANET>
Any ideas?
P.S.: git clone from the same git repo address works perfectly.
Ok, as of now, npm does not support socks proxy: https://github.com/npm/npm/issues/6204
I now use ProxyCap.
http://www.proxycap.com
Works great. It will manage the ssh tunnel + socks proxy and route all traffic automatically (also for npm) through it.
Proxifier works for me. Simply add a proxy and npm install ...
.com/mac/
You can use polipo to convert sock5 proxy to http proxy, and it's super convenient if you're under Linux.
You can reference this page for further detail:
Convert Socks Proxy To Http Proxy: Using Polipo
For a workaround that works on both Linux and Windows, you can use http-proxy-to-socks.
$ npm install -g http-proxy-to-socks
$ hpts -s 127.0.0.1:1080 -p 8080
Now you have you http proxy at port 8080.
Use v2rayN as client.
It will launch both socks5 and http proxy.
Then you can use its http proxy for npm proxy and socks5 for other proxy at same time.
// http for npm
npm config set http-proxy http://127.0.0.1:10809
// socks5 for git
git config --global http.proxy 'socks5://127.0.0.1:10808
v2raN UI Screen Shot

set npm proxy without using http:// before the proxy server name

I have npm installed and have used it a few times but unfortunately I had to change my proxy and I am not able to get it to work again.
here are my past settings:(the ones that worked)
npm config get proxy: http://proxy-foo.foobar.com:8080
npm config get https-proxy: http://proxy-foo.foobar.com:8080
now my proxy is a server name and when I use the proxy in my browser it works fine, but when I set the config in npm it fails with 'getaddrinfo ENOTFOUND'
current settings:
npm config get proxy: http://servername:8080
npm config get https-proxy: http://servername:8080
in my browsers proxy I do not use http:// before the server name and I think this is what is causing it to fail. could this be the problem and if so is there a way to set proxy configs in npm without using http:// before.
You're right; npm (more accurately, request, which actually does the fetching) does care about the http:// before the proxy name.
The proxy setting should be a fully-qualified URL that you could visit in a browser, e.g.,
http://proxy.company.com:port/
or
http://1.1.1.1:1234/
if specified as an IP address.

Getting Node.io to work through a proxy

I'm trying to use node.io (web scraping module) through a proxy. I know it has untested support for proxies built in, but I cannot get it to work. Does anyone know how to get it working?
Thanks,
You have to configure npm to make node to use a proxy server;
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080'

Resources