Learn npm runtime configuration file| .npmrc file example

Did you observe nodejs project has several RC dotfiles like .npmrc, and .babelrc generated in a nodejs project?

In this tutorial, learn about the contents of npmrc with the below things

  • npmrc file create
  • how to add a registry and scope multiple registries
  • npm config set, get the list
  • npmrc auth token configuration
  • npmrc file location in windows
  • How to create an npm runtime configuration
  • npmrc sample file example
  • parsing RC file in nodejs

For example, We have different RC files in different applications

.npmrc .babelrc .netrc .vuerc .yarnrc

npmrc dotfiles

.npmrc is an npm runtime configuration file that contains the following things related to the nodejs project as well as the application. This will be used by npm commands to run based on configured settings

  • environment variables
  • npm registry configuration
  • auth configurations

normally you can pass command-line options to the npm command. This is a command level.

suppose you want to change the log level for the npm install command.

npm install primeng --loglevel silent

The same above command level options can be replaced by placing loglevel in the npmrc file as below

loglevel=silent

In the same way, we can configure different settings in this file🔗 .npmrc file can be created as

  • globally
  • OS user
  • Application-level rc files are configuration settings for a module or a project in a Unix operating system.

you can list out the npmrc file content using the below command

npm config list

you can update the npmrc with the npm config set command

npm config set author.name "John"
npm config set author.email "[email protected]"

configuration settings can be retrieved with the npm config get command

npm config get author. name
npm config get author.email

.npmrc file can be configured to have environment variable

What is the .npmrc file location in Windows and MAC?

In Windows, Let’s see the path of the file location.

.npmrc file is created in windows for nodejs installation globally with below location

C:\Users\{username}\AppData\Roaming\npm

if you want settings such as logging need at the OS user level, you can find below the location

C:\Users\{username}\.npmrc

Another location for npmrc will be created while installation of nodejs setup.

C:\Program Files\nodejs\node_modules\npm\

also

you can also check npm location on any OS using the below command

npm config ls -l

How to create an npmrc file?

You can create npmrc files using the below approaches

  • manually with any editor
  • npm login command

.npmrc file content can be similar to the ini file format

change loglevel for npm install command output

npm install command gives a lot of useful information to the console.

This can be controlled with loglevel settings via command line or npmrc loglevel settings

npm install lodash --loglevel silent

you can set the command to update the log level.

npm config set loglevel="silent"

This update the npmrc file with the below entries

loglevel=silent

There are other loglevel settings like silent, HTTP,warn,info,error, andsilly`
npmrc environment variables

How to update proxy configuration in npmrc file?

npm config command has a set command to set the below values with proxy url addresses

  • http-proxy
  • https-proxy
  • proxy details
npm config set http-proxy http://username:password@domain:port
npm config set https-proxy http://username:password@domain:port
npm config set proxy http://username:password@domain:port

npmrc multiple registries

registration is a repository of open-source javascript npm libraries which can be used by dependent application users.

nodejs uses the default registry for npm packages to download dependencies.

you can create custom scope packages with the below command

npm config set @cloudhadoop: registry http://npm.cloudhadoop.com

You can create and configure multiple registries in the npmrc file

Here is a code for npmrc multiple registry example

registry=https://registry.npmjs.org/
@cloudhadoop:registry=http://npm.cloudhadoop.com

npmrc auth token

sometimes, We want to add auth token to authorize the registration of scope modules.

For example, font awesome comes with a free and licensed version.

the free version can be downloaded directly without auth token.

But for the licensed version, you need to supply auth token which is a secret key.

So you will not directly configure in npmrc file

first, create an environment variable for auth_token

export $AUTH_TOKEN=123

Next run below command

@fontawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:\_authToken=$AUTH_TOKEN

authorization token will not be committed to a git repository.

You can also add in .env files if you are using env files in your project which are not committed to git due to this is added in .gitignore by default.

npmrc parser in nodejs

There are multiple libraries to parse RC files similarly to a JSON file,

There are npm libraries like rc🔗 used to parse from javascript code.

npmrc sample file example

Let’s see a sample npmrc file in the node project.

comments always started with # or ; that will be ignored by npm commands

# configure auth token

@fontawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:\_authToken=$AUTH_TOKEN

#Default registry
registry=https://registry.npmjs.org/
#scoped registry
@cloudhadoop:registry=http://npm.cloudhadoop.com

;log level settigns
loglevel=warn

Conclusion

In this tutorial, You learned about npmrc files with examples in nodejs projects.