0
0
NodejsHow-ToBeginner · 3 min read

How to Use .npmrc File in Node.js for Configuration

Use a .npmrc file in Node.js to customize npm behavior by setting configuration options like registry URLs, authentication tokens, or proxy settings. Place the .npmrc file in your project root or user home directory to apply settings locally or globally.
📐

Syntax

The .npmrc file contains key-value pairs in the format key=value. Each line sets one configuration option for npm.

  • key: The npm setting name (e.g., registry, proxy).
  • value: The value for that setting (e.g., URL, token).

Example line: registry=https://registry.npmjs.org/

ini
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=YOUR_TOKEN_HERE
proxy=http://proxy.example.com:8080
💻

Example

This example shows a .npmrc file that sets a custom registry and an authentication token for private packages. It demonstrates how npm uses these settings when installing packages.

ini
registry=https://custom.registry.com/
//custom.registry.com/:_authToken=abc123token

// Run npm install in your project folder
// npm will use the custom registry and token automatically
Output
npm install will fetch packages from https://custom.registry.com/ using the provided auth token.
⚠️

Common Pitfalls

Common mistakes when using .npmrc include:

  • Placing the .npmrc file in the wrong directory, so npm does not read it.
  • Incorrect syntax like missing equal signs or spaces around =.
  • Exposing sensitive tokens publicly by committing .npmrc to version control.
  • Conflicts between global and local .npmrc files causing unexpected behavior.

Always check your file location and syntax, and use environment variables or npm login for sensitive data when possible.

ini
Wrong:
registry https://registry.npmjs.org/

Right:
registry=https://registry.npmjs.org/
📊

Quick Reference

Here is a quick reference for common .npmrc settings:

SettingDescriptionExample Value
registryURL of npm package registryhttps://registry.npmjs.org/
proxyHTTP proxy URLhttp://proxy.example.com:8080
https-proxyHTTPS proxy URLhttps://proxy.example.com:8080
_authTokenAuthentication token for private registriesabc123token
strict-sslEnable strict SSL verificationtrue or false

Key Takeaways

Place .npmrc in your project root for local settings or in your home directory for global settings.
Use key=value syntax without spaces for each configuration line in .npmrc.
Keep sensitive tokens out of version control to protect your credentials.
Local .npmrc settings override global ones, so check both if npm behaves unexpectedly.
Use .npmrc to customize npm behavior like registries, proxies, and authentication easily.