본문 바로가기
  • AI (Artificial Intelligence)
Programming/JavaScript, Node.js

What is the .NPMRC file for?

by 로샤스 2020. 8. 11.

Ref. https://medium.com/@ngubanethabo.ambrose/what-is-the-npmrc-file-for-d19fe6d69c9a

A quick look into what this file is used for in a NPM project.

As you might know already, NPM is a powerful package manager for Node/JS applications. If you do not know what Node/JS/NPM is, check out these links NodeJS, NPM and JavaScript

In this article, I assume you have knowledge of Node/JS/NPM, and that you know how to use it, and you have an idea of what an INI file format is and what it entails.

What is the .npmrc file?

This file is a configuration file for NPM, it defines the settings on how NPM should behave when running commands.

What does the rc mean on configuration files, like for example .bowerrc?

It definitely does not mean Release candidate.

The rc, taking from a ST post, can stand for various things, but I will take that in this context, it stands for Runtime configuration.

There are several ways to configure NPM. You can do it via:

  • The command line by passing a setting with the NPM command you want to run.
  • Through ENV variables
  • And through..guess what.. npmrc

I will only focus on one example on how you would set a specific flag/setting depending on the context of the command execution, that will be the loglevel flag.

If you are working with NPM scripts, you might have noticed that when running a script there is some text echoed before the results of the underlying script, for example, check the execution of a JS script that adds two numbers:

NPM script run

Notice that text? Something about the project details and script name, but anyway.

To configure NPM not to show this text when running scripts you can instead pass the loglevel flag along with the script’s execution, like below:

run silent

See how the text is now suppressed, yeah that is setting NPM via the command line, so the -silent flag is a value for the loglevel setting.

How would you do that via the .npmrc file?

In the root of your project, create a .npmrc and set the loglevel setting value like below:

npmrc file

And that is it! now you do not have to supply the -silent flag when running your script.

npm run hello-js

To set via the ENV variables I did the following:

env var

I am not certain that this works, I did not reboot my system, it did not work when testing as I am guessing a system reboot is required for the ENV var to take effect.

댓글