When working with npm (Node Package Manager), you might need to configure a proxy if you are behind a corporate firewall or if you’re accessing the internet through a proxy server. Here’s how you can configure npm to use a proxy, such as http or https, socks5,.etc, Also, you can set a custom or private local registry for npm.
Setting up a proxy for npm:
- View Current Configurations:
To check your current npm configuration, you can use the following command:npm config list
- Set Proxy:
You can set the proxy using thenpm config set
command. Replace the placeholders with your proxy information.npm config set proxy http://your-proxy-server:proxy-port
npm config set https-proxy http://your-proxy-server:proxy-port
- Username and Password for Proxy:
If your proxy requires a username and password, you can set them using the following commands:npm config set proxy http://username:password@your-proxy-server:proxy-port
npm config set https-proxy http://username:password@your-proxy-server:proxy-port
- Clear Proxy Configuration:
If you want to remove the proxy configuration, you can use thenpm config delete
command:npm config delete proxy
npm config delete https-proxy
Example:
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
Configuration via Environment Variables:
You can also set the proxy configuration using environment variables:
export HTTP_PROXY=http://your-proxy-server:proxy-port
export HTTPS_PROXY=http://your-proxy-server:proxy-port
Verify Configuration:
You can verify your npm configuration by running:
npm config list
Ensure that the proxy settings are correctly displayed in the output.
Remember to replace placeholders like your-proxy-server
, proxy-port
, username
, and password
with your actual proxy details.
Note: If you’re behind a firewall, you might also need to configure npm to use the appropriate registry. You can set it using:
npm config set registry https://registry.npmjs.org/
Adjust the registry URL based on your requirements.
Always be cautious when providing sensitive information like usernames and passwords, especially in command-line arguments, as they may be stored in your command history. Consider using environment variables or a more secure method for handling credentials in production scenarios.