Learn npm runtime configuration file| .npmrc file example


Did you observed nodejs project has sevaral rc dot files like .npmrc, .babelrc in a project

In this tutorial, Learn about contents of npmrc with below things

  • npmrc file create
  • how to add registry and scoped multiple registries
  • npm config set,get list
  • npmrc authtoken configuration
  • npmrc file location in windows
  • How to create a 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 dot files

.npmrc is a npm runtime configuration file which contains following things related to nodejs project as well as 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 npm command. This is an command level.

suppose you want to change loglevel for npm install command.

npm install  primeng --loglevel silent

The same above command level options can be replaced by placing loglevel in 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 an module or a project in a unix operating system.

you can list out the npmrc file content

npm config list

you can update the npmrc with npm config set command

npm config set author.name "John"
npm config set author.email "John@cloudhadoop.com"

configuration settings can be retrieved with npm config get command

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

.npmrc file can be configured to have environment variable

.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 to be applied at logged in OS user level, you can find below 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 below command

 npm config ls -l

How to create a npmrc file

You can create npmrc files using below approaches

  • manually with any editor
  • npm login command

.npmrc file content can be like similar of ini file format

change loglevel for npm install command output

npm install command gives lot of useful information to console.

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

npm install lodash --loglevel silent

you can set with command to update loglevel

npm config set loglevel="silent" 

This will updates the npmrc file with below entries

loglevel=silent

There are other loglevel settings like silent,http,warn,info, error and silly
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 registry

registration is an repository of opensource javascript npm libraries which can be used by dependent application users.

nodejs uses default registry for npm packages to download dependencies.

you can create a custom scope packages with below command

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

You can create and configure multiple registry in npmrc file

Here is an 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, fontawesome comes with free and licensed version.

free version can be downloaded directly without auth token.

But for licensed version, you need to supply auth token which is an secrent 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 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

Like a json files, There are multiple libraries to parse rc files.

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

npmrc sample file example

Let’s see an sample npmrc file

comments always started with # or ; which 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 file with examples in nodejs projects

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.