What is the .npmrc File?
The `.npmrc` file is an npm (Node Package Manager) configuration file that allows users to customize the behavior of npm. This file can be used to specify the default settings for npm commands or to configure settings for a specific project. The `.npmrc` file can exist at various levels such as user environment, project level, and global level, each with different priority.
The main purposes of the `.npmrc` file are as follows:
1. Registry Configuration
You can specify the registry to use when downloading or uploading npm packages. For example, you can set a private registry used within your company.
registry=https://custom-registry.example.com/
2. Authentication Configuration
You can set the authentication token or credentials needed to access the registry.
//registry.example.com/:_authToken=YOUR_AUTH_TOKEN
3. Cache Configuration
You can set the npm cache directory or adjust various options related to caching.
cache=~/.npm/custom-cache
4. Proxy Configuration
You can configure the proxy settings if you need to access the internet through a proxy in your company’s or organization’s network environment.
proxy=http://proxy.example.com:8080
https-proxy=http://proxy.example.com:8080
5. Package Installation Options
You can set specific flags as default when installing packages. For example, you can disable the creation of `package-lock` files or configure npm to not install developer dependencies.
package-lock=false
save-exact=true
6. Script Execution Environment Configuration
You can set the environment variables in which npm scripts are executed.
scripts-prepend-node-path=auto
Locations and Priorities of the .npmrc File
- Global Configuration: The `.npmrc` file located in the npm installation path applies to all npm commands. (`$PREFIX/etc/npmrc`)
- User Configuration: The `.npmrc` file located in the user’s home directory applies to a specific user. (`~/.npmrc`)
- Project Configuration: The `.npmrc` file located in the root directory of a project applies to a specific project. (`<project>/npmrc`)
- Default Configuration: The `.npmrc` file located in the npm installation path defines the default settings of npm. (`$(npm root -g)/npm/npmrc`)
These settings can be defined in various locations, and npm reads and applies these configuration files in order of priority. Project settings take precedence over user settings, and user settings take precedence over global settings.
By using the `.npmrc` file in this way, you can finely tune the behavior of npm and create an environment tailored to your project.