56

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?

Maxim Mazurok
  • 3,856
  • 2
  • 22
  • 37
Shaun Xu
  • 4,476
  • 2
  • 27
  • 41

5 Answers5

31

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();
Luke
  • 186
  • 1
  • 7
apsillers
  • 112,806
  • 17
  • 235
  • 239
6

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

Zheng Bowen
  • 339
  • 2
  • 7
  • Is this good to retrieve the URLs I visit in my browser? – Berstigma May 13 '21 at 21:49
  • `"https://localhost:1080"` yields error. Says must start with `http://`. – izogfif Sep 22 '21 at 04:58
  • @izogfif facing same issue any solution ? – shivshankar Apr 25 '22 at 02:50
  • @shivshankar It's been a long time ago, but I somehow managed to make it work. According to `package-lock.json`, the working version was `1.0.1`. The working `index.js` has these lines: `const proxy = require("node-global-proxy").default; proxy.setConfig({http: "http://localhost:8888", https: "http://localhost:8888"}); proxy.start();` – izogfif Apr 26 '22 at 13:01
  • I managed to make it work. unfortunately, it doesn't have any proxy bypass option. so I had to revert back – Shahyad Sharghi Apr 28 '22 at 01:49
  • @izogfif Change https to http will work. like `https: "http://localhost:1080"` – Philip Jul 04 '22 at 07:31
  • @Philip Ehm, that's what I wrote in my comment. Both http and https point to the same address: `http://localhost:8888` – izogfif Jul 04 '22 at 07:37
4

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

Undo
  • 25,519
  • 37
  • 106
  • 129
Shaun Xu
  • 4,476
  • 2
  • 27
  • 41
  • 1
    Great solution, I am using a third part middleware, it will make http request, how to resolve proxy issue in this scenario? – yuyue007 Apr 30 '14 at 07:08
  • I think the code in my blog can solve your problem if the 3rd party module utilizes `http` module to send request. Just ensure you `require` my module before the 3rd party one. – Shaun Xu May 04 '14 at 02:07
  • Url return 404, I found this one http://geekswithblogs.net/shaunxu/archive/2013/09/05/semi-global-proxy-setting-for-node.js.aspx – haxpor Feb 06 '17 at 17:17
3

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])

Megh Parikh
  • 924
  • 2
  • 7
  • 25
-5

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
vijay
  • 609
  • 5
  • 9
  • Note that {userid} and {password} need to be URL-encoded, if they contain meta-characters (like "/", ":", "@", ...) – Fred Danna Nov 23 '21 at 21:54
  • This is already addressed in the question. This question is about a node.js proxy, not an npm proxy. – Amit Naidu Apr 19 '22 at 22:13