DEV Community

MoreOnFew
MoreOnFew

Posted on • Originally published at moreonfew.com on

How to set proxy using npm config in NodeJs?

While working with NodeJs you’ll notice that many times you might not be able to install or update while working behind a proxy network like the corporate web proxy of your office etc. Basically, you might notice that the commands like npm install is not working. However, this can be easily fixed by setting the proxy of NodeJs using config command. You would have to set the proxy for both http and https proxy.

In NodeJs the npm uses a configuration file that can be easily edited using the npm config edit command. You can use that to set the proxy values directly into the file. As an alternative, you can also use the npm config set <key> <value> command to set the http-proxy value. Following is the code you would require to set the proxy using the npm config in NodeJs :

npm config set proxy http://your-company-proxy.com:8080
npm config set https-proxy http://your-company-proxy.com:8080
Enter fullscreen mode Exit fullscreen mode

If your network requires a username password then please pass it in like :

npm config set https-proxy http://username:password@your-company-proxy.com:8080
Enter fullscreen mode Exit fullscreen mode

How to encode special characters in password in npm?

Also, remember to you might have to url encode the proxy url if there are special character in it. Especially if you have characters like “@” within your password, you might have to replace it with a relevant Hex value from the ASCII code list.

For Example, if the password contained an ” @ ” symbol like “abc@xyz” then you’ll have to pass the password as “abc %40 xyz”. You need to replace the special character with its equivalent HEX value that you can pick up from the following table of ASCII Codes. You need to prefix a percent sign ” % ” to the HEX value given in the table.

With special characters escaped, the config command might look like the following if your password was “abc @ xyz”:

npm config set https-proxy http://username:abc%40xyz@your-company-proxy.com:8080
Enter fullscreen mode Exit fullscreen mode

Hope this article was helpful to you and you were able to set the proxy on nodeJs using npm. Let us know in the comments.

The post How to set proxy using npm config in NodeJs? first appeared on MoreOnFew.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay