Node.js, NPM, proxy and node_modules

2019-01-15 22:52发布

问题:

I am trying to install node packages but my network won't let me connect. I get the standard,

npm ERR! network getaddrinfo ENOTFOUND

I understand that this is because I am on a corporate network. However, I'm not very familiar with proxies and as far as I am aware, I don't have access to one so I'm not sure how I can progress with NPM. Any helpful and related thoughts about this are warmly received!

As a work around, using my other machine, I have emailed myself with the node modules I require. These node modules were installed using NPM while not on the corporate network.

However, despite the fact that I have put the modules in node_modules/ alongside my test_server.js file, I still receive,

C:\Users\JT15900\Documents\nodeServer>node test_server.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'knex' ... etc.

To be really clear, test_server.js sits alongside node_modules/ in the same directory. Inside node_modules/ I have put the various module folders like 'express' and 'knex', etc.

Why is my node script unable to find these modules and how to I correct it?

EDIT

I used a post over at superuser to help discover my proxy settings. I then configured Node NPM proxy settings as outlined elsewhere and presented in the solution.

回答1:

This helped me http://jjasonclark.com/how-to-setup-node-behind-web-proxy

if the corporate network has a proxy you can set it in npm using these commands

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

Apologies if you already tried that or it doesn't work.



回答2:

This worked for me post setting proxy -

Change the registry to the https version using the following command

npm config set registry https://registry.npmjs.org/