How to configure node-fetch to use the company proxy?

Ashok picture Ashok · Feb 11, 2020 · Viewed 10.6k times · Source

I am executing a standalone nodejs script (no web server involved) that needs to fetch result from a third party api. The program uses 'node-fetch' to do the fetch(url) and I run it using node .\test.js from command line.

It fails when I am connected to our company network but works fine on direct internet. I have configured the proxy settings in npm and could see that npm config ls shows the correct values for proxy and https-proxy.

So the questions are: 1. Does running the test.js via node not pick the proxy config from npm? 2. How to make sure that the fetch(url) call goes through our proxy?

Thanks in advance

Answer

Abinash picture Abinash · Feb 11, 2020

This worked for me, try using this : https://github.com/TooTallNate/node-http-proxy-agent

The request formed will be similar to this:

fetch('accessUrl', {agent: new HttpsProxyAgent('proxyHost:proxyPort')})
    .then(function (res) {
    })