Node.js global proxy setting
Asked Answered
K

6

61

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?

Kerrikerrie answered 3/9, 2013 at 7:52 Comment(1)
As of Node.js v12, you can use github.com/gajus/global-agent.Mansur
G
33

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();
Garygarza answered 7/1, 2015 at 18:2 Comment(2)
so there is a global mechanism for proxy settings? because that's basically what the env.http_proxy is, no?Pahlavi
Can username and password be specified in the global tunnel configuration?Councilor
F
8

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

Felicitation answered 3/6, 2020 at 14:10 Comment(7)
Is this good to retrieve the URLs I visit in my browser?Fraxinella
"https://localhost:1080" yields error. Says must start with http://.Oliy
@Oliy facing same issue any solution ?Manassas
@Manassas 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();Oliy
I managed to make it work. unfortunately, it doesn't have any proxy bypass option. so I had to revert backSternmost
@Oliy Change https to http will work. like https: "http://localhost:1080"Acanthus
@Acanthus Ehm, that's what I wrote in my comment. Both http and https point to the same address: http://localhost:8888Oliy
K
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

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

Branum answered 27/7, 2017 at 15:49 Comment(0)
G
-1

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

Gio answered 16/4 at 2:32 Comment(0)
S
-6

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
Synonym answered 2/4, 2019 at 12:37 Comment(2)
Note that {userid} and {password} need to be URL-encoded, if they contain meta-characters (like "/", ":", "@", ...)Janaye
This is already addressed in the question. This question is about a node.js proxy, not an npm proxy.Curative

© 2022 - 2024 — McMap. All rights reserved.