63

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?

1

6 Answers 6

35

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();
Sign up to request clarification or add additional context in comments.

4 Comments

so there is a global mechanism for proxy settings? because that's basically what the env.http_proxy is, no?
Can username and password be specified in the global tunnel configuration?
@MichaelMcLaughlin not globally for Node, the conventional http_proxy env variable is only supported indirectly via global-tunnel-ng mechanisms to override behavior for an individual Node process.
@AnirudhJayakumar yes, the conventional http_proxy env variable supported by tools like curl accepts a "user:pwd@" prefix: oxylabs.io/blog/curl-with-proxy#using-environment-variables. global-tunnel-ng should respect username/password specified there too.
11

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

7 Comments

Is this good to retrieve the URLs I visit in my browser?
"https://localhost:1080" yields error. Says must start with http://.
@izogfif facing same issue any solution ?
@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();
I managed to make it work. unfortunately, it doesn't have any proxy bypass option. so I had to revert back
|
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

3 Comments

Great solution, I am using a third part middleware, it will make http request, how to resolve proxy issue in this scenario?
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.
Url return 404, I found this one geekswithblogs.net/shaunxu/archive/2013/09/05/…
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])

Comments

-2

In my case, I go to folder C:\Users\my-username and edit file .npmrc.

proxy=http://my-username:my-password@proxy-host:proxy-port
http-proxy=http://my-username:my-password@proxy-host:proxy-port
https-proxy=http://my-username:my-password@proxy-host:proxy-port

Everything work ok

Comments

-7

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

2 Comments

Note that {userid} and {password} need to be URL-encoded, if they contain meta-characters (like "/", ":", "@", ...)
This is already addressed in the question. This question is about a node.js proxy, not an npm proxy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.