Setup a Default NPM Global Settings for New Projects

Introduction

Are you an aspiring web developer, or have you been a web developer for quite some time now? You have probably used either Yarn or Node Package Manager (NPM) as your package dependency manager. Moreover, within the space of NPM, we can create a new project with the help of the npm init command as it generates a package.json file inside our project. As we all know, it is a critical part because it identifies the metadata of our project.

However, creating a new project by consistently executing the npm init or npm init –y without really setting up your default setting is quite time-consuming. That’s why in this article, we’re going to show you how we can set up the NPM global settings for you to create a new project with your desired configuration quickly.

In other words, if you’re using npm init –y, this instantly creates a project for you, but you’re doing several projects. Later on, you decided to edit the essential information (author, email, license, etc.) in your package.json file, which is time-consuming. Why not configure the NPM global default values that npm init will use when creating a new project.

OK, then let’s get started.

What is the npmrc file?

This npmrc file is a configuration file for NPM because it defines how NPM should behave when running specific commands. Moreover, we can then say that this is the runtime configuration file of NPM.

Where can we find the npmrc file?

There are four relevant files for the npmrc file

  • Per project config file
  • Per-user config file
  • Global config file
  • Npm builtin config file

You can find your npmrc file instances using this command npm config ls -l | findstr config. You can use grep instead of findstr if you’re in the Linux command line.

Let’s see the screenshot below.

Setup a default NPM global settings for new projects figure 1.
Figure 1

Configuring Global NPM Config

In this section, we’ll be focusing on how we can configure the NPM global settings.

You can try to execute the command npm config set <key> <value> (if you want to set a value) or npm config get <key> (if you want to get a value).

Let’s see the screenshot below on how to use the npm config further.

Setup a default NPM global settings for new projects figure 2.
Figure 2

As you can see, you can use the --help if you need to explore the command a bit further.

Moreover, I know what you are thinking. You have already got a clue, and you’re right. Using the keyword get and set, we can then change and see the desired settings that we’re interested in.

However, let’s first see the default settings needed in a project.

I believe if we want to see, we can try by creating a new project.

Let’s see a screenshot below.

Setup a default NPM global settings for new projects figure 3.
Figure 3

With the screenshot that we have, we can say that the author’s information such as email, name, URL must be set to its default, including the license and version.

However, before we configure those settings, let’s try to see the equivalent key of those in the default configuration of the npmrc file. You can do this by this executing the command: npm config ls -l, but this command gives a long list of the default configuration.

You can then execute the command: npm config ls -l | findstr init to lessen the result. You can use grep if you’re on the Linux command line instead of findstr.

Figure 4

Let’s see how we can configure that.

We can use the command npm config set init-author-name <value>.

Let’s see an example below.

Figure 5

Now, let’s try to get the values of these settings.

Let’s see the screenshot below.

Figure 6

Now that we have set up the default settings, why not try to create a new project, and let’s see if the default settings will take effect immediately.

Let’s see the screenshot below.

Figure 7

Summary

This article has shown how we can find the npmrc configuration file, configure the NPM default configuration settings, and how it affects creating a new project using the npm init command.

I hope you have enjoyed this article. Stay tuned for more. Until next time, happy programming!

Please don’t forget to bookmark, like, and comment. Cheers! And Thank you!