0

I found already a lot of helpful tutorials to set the proxy locally and global to install packages and so on. Now I started a new project and I figured out how to reuse the proxy settings:

#! /usr/bin/env node

var http = require("http");
var shell = require('shelljs');
var request = require('request');
var iplocation = require('iplocation')

// setup proxy
var proxyUrl = shell.exec('npm config get proxy', {silent:true}).stdout;
var proxiedRequest = request.defaults({
  'proxy': proxyUrl, 
  'https-proxy' : proxyUrl,
  'strict-ssl' : false
});

// get location (works)
  proxiedRequest('http://ipinfo.io/216.58.194.46', function (error, response, body) {
  console.log('error:', error);
  console.log('statusCode:', response && response.statusCode);
  console.log('body:', body); 
});

// doesn't work 
iplocation('56.70.97.8').then(res => {
  console.log(res.iplocation)
  })
  .catch(err => {
    console.error(err);
})

Is there a way to set it someway global for the project so other npm packages could use it too? I tried a local .npmrc file in the projects folder but it doesn't affect the environment at all.

Any hints are welcome. Thanks

Rubén
  • 34,714
  • 9
  • 70
  • 166
Johannes Knust
  • 891
  • 1
  • 11
  • 18

1 Answers1

0

This SO answer1, SO answer2 explains different ways to set npm proxy. See if it helps you.

You could add functions like proxy_on and proxy_off to your bashrc which will let you set global npm config and toggle it from your command line.

Refer to this gist for the code.

Anu
  • 1,079
  • 8
  • 12
  • I always want to use the proxy. I have an `.npmrc` file which includes the proxy settings. I expect the project is recognising there is some proxy setting and use it every time for a request. – Johannes Knust May 07 '18 at 10:13
  • @john Can you post your npmrc? And what exactly is the issue? Is the proxy settings not being picked up from your npmrc? – Anu May 07 '18 at 10:28
  • `strict-ssl=false proxy=:@: https-proxy=:@: registry=http://registry.npmjs.org/ ` basically it is working because I can install packages I just don't get the step to use this proxy settings within my project for example here `iplocation('56.70.97.8').then(res => {` – Johannes Knust May 07 '18 at 10:43