DEV Community

Rinon Tendrinomena
Rinon Tendrinomena

Posted on

NPM Config: customising how npm works

When we’re working with Node.js and npm, knowing how to adjust your settings can help make your work smoother. npm lets us change its settings through something called npm config, which helps control how npm behaves. These settings are saved in a file called .npmrc.

In this article, we’ll explain how npm config works, why it’s useful, and how you can use it to make npm fit your needs.

What Is npm config?

npm config is a way to adjust settings for npm so it works the way you want. You can use it to change things like:

  • Which website npm uses to download packages (the registry)
  • How npm saves packages in your project
  • Proxy settings for working behind a firewall

These settings are saved in a file called .npmrc, and npm looks at this file whenever you run npm commands.

How to Use npm config?

Some basic npm config Commands that developers should know about:

You can change npm settings using the npm config command. Here are some basic commands you can use:

- Set a new setting:

npm config set <key> <value>
Enter fullscreen mode Exit fullscreen mode

For example, to change the registry npm uses to download packages, you can run:

npm config set registry https://registry.npmjs.org/
Enter fullscreen mode Exit fullscreen mode

- Get a setting: If you want to see the current value of a setting, use:

npm config get <key>
Enter fullscreen mode Exit fullscreen mode

- List all settings: To see all current npm settings, run:

npm config list
Enter fullscreen mode Exit fullscreen mode

- Delete a setting: If you want to remove a setting, use

npm config delete <key>
Enter fullscreen mode Exit fullscreen mode

- Edit the .npmrc file: You can open the .npmrc file directly to make changes by using:

npm config edit
Enter fullscreen mode Exit fullscreen mode

Configuration Scopes (Where Settings Apply)

npm settings can apply in different places:

1. Global Settings:

These apply to all projects on your computer and are saved in a file in your home directory (~/.npmrc). To make a global setting, add the -g flag:

npm config set <key> <value> -g
Enter fullscreen mode Exit fullscreen mode

2. Project Settings:

These settings apply only to the project you're working on. npm will look for a .npmrc file in the project folder.

3. User Settings:

These settings apply to all the projects under your user account.

4. Environment Settings:

These are temporary settings you can use when running npm commands. They’re often used in build pipelines or different environments.

Common npm Configurations

1. Registry:

By default, npm uses https://registry.npmjs.org/ to get packages. You can change this to a private registry if needed:

npm config set registry https://custom-registry.example.com
Enter fullscreen mode Exit fullscreen mode

2. Proxy:

If you work behind a proxy (like in some company networks), you can set the proxy with npm:

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

3. Cache:

npm saves downloaded packages in a cache to make things faster. You can change where npm stores this cache:

npm config set cache /path/to/npm-cache
Enter fullscreen mode Exit fullscreen mode

4. Strict SSL:

If you need to use self-signed certificates, you might have to turn off npm's strict SSL checking:

npm config set strict-ssl false
Enter fullscreen mode Exit fullscreen mode

5. Save Options:

When you install packages, npm can save them in your package.json file. You can set npm to always save the exact version of a package:

npm config set save-exact true
Enter fullscreen mode Exit fullscreen mode

6. Authentication Tokens:

If you're using private registries, you may need to set an authentication token:

npm config set //registry.example.com/:_authToken=YOUR_TOKEN
Enter fullscreen mode Exit fullscreen mode

Working with Multiple .npmrc Files

You can have different .npmrc files for different purposes, allowing you to customize npm for global use, specific projects, or even temporary environments.

- Global Configuration:

This applies to everything and is usually stored in $HOME/.npmrc.

- Project Configuration:

A .npmrc file in your project folder will only apply to that project.

- User Configuration:

Settings that apply to all projects for your user account are stored in your home directory(~/.npmrc).

- Environment Variables:

You can also override settings by using environment variables, which are useful in CI/CD or development environments.

The priority order in which these configurations are applied is:

Command Line Flags: Any settings passed as flags directly into npm commands.

- Project .npmrc: Project-specific settings.
- User .npmrc: User-specific settings.
- Global .npmrc: Global settings.
- Default Settings: npm's default configurations.

This hierarchy ensures that project-specific configurations take precedence, allowing different settings for different projects.

Tips for Using npm config

1. Different Registries for Different Projects:

If you're working on multiple projects, you can set a different registry for each by placing a .npmrc file in each project's root folder.

3. Environment Variables:

You can set npm config values as environment variables in your terminal session, like this:

NPM_CONFIG_REGISTRY=https://registry.example.com
Enter fullscreen mode Exit fullscreen mode

3. Switching Configurations:

You can switch between different .npmrc files if you're working in different environments (for example, development vs production).

Understanding and using npm config helps us customize how npm behaves, making it easier to manage different projects, private registries, proxies, and more. Whether we’re working on a single project or many, npm config gives us the flexibility to make npm work exactly how we need it.

By using .npmrc files, we can ensure consistent settings across projects and teams, saving us time and making development smoother.

Since we've just learnt more about npm config today, next time we run into a problem or need something special from npm, check if adjusting the config can help!

As always, thank you so much for reading and if you find this post helpful, share it with your friends.

Top comments (0)