googleads
Guide to use the Node Package Manager (npm) - Hire NodeJs Developer
preloder
Code Study Javascript FrameWorks Technical Tips & Tricks

Guide to use the Node Package Manager (npm)

Node Package Manager – Introduction:

In this article, we are going to see how to use the Node Package Manager.
JavaScript is easy and most of the people use a client-side scripting language in websites development and web applications. It has the maximum number of libraries.
Previously, these libraries were quite few in number and hence, were easy to maintain. But now we have a lot of libraries mushroomed. So we need some dependency management mechanism and sometimes other solutions, in order to manage these libraries.
For this purpose of maintaining the vast libraries, we will be using the Node Package Manager (npm).

What is Node Package Manager?

Node Package Manager (npm) is a JavaScript package manager mostly (of course!) used for Node.js, although it can also be used independently. Npm makes it is easy for JavaScript developers to share and reuse code, and it also makes it easy to update the code that you are sharing. We have some CLI commands to handle the package manager.
You can simply run the below commands to inject JavaScript package files into your projects.

 

In order to install a specific version run the below command.

 

You can install packages globally (like Mocha, or Angular-CLI) simply by adding -g in the command.

 

Mostly the command <npm install>l is enough to download, install and configure the JavaScript packages. But npm has much more than that. It has many additional options to manage the package. Below, I have explained these options.

npm CLI commands:

CLI (command-line interface) interface is a way to interact with a computer by typing text commands into a terminal window.
Run npm help to get a list of all available commands and running npm help-search <search-text> will give a list of options available for the “search-text”. See the core command options below.

1. install:

o As is obvious, this will install a new package locally or globally ( -g). Alternatively you can install dependencies packages list in package.json file. Syntax as below:

 

2. uninstall:

Needless to explain, this is to remove (or we can say, purge) a specific package form node_modules folder locally or globally( -g).  Syntax as below:

 

3. access:

This command is used to play with user permissions within the context of npm organizations packages. It is used with adduser, owner, team, etc., and gives grained control to access users. Syntax as below:

 

4. bin:

This is used in order to display the npm installation path. On running this command you can see the absolute file path. Syntax as below:

 

5. cache:

This command handles the npm cache folder,  wherein it allows you to add, clean or list the npm cache folder from the installation directory. The “ls” subcommand is used to list cache data, and the “add” subcommand is used to add the specific package locally, while the  “clean” subcommand is used to clear all the package cache data. Syntax as below:

 

6. config:

This command deals with npm configuration settings locally or globally. Using its subcommand we can set, get, list, edit or delete the users or global npm configuration. Syntax as below:

 

7. dedupe or ddp:

With this command, we can simplify the overall structure of locally installed packages based on its dependencies so as to manage the installed packages better.

 

8. link:

The link command is used to create a symbolic link for own npm packages. This link can then be used even globally, and so it can be tested as globally installed packages from the npm registry.

 

9. ls:

As is with most cases, the ‘ls’ command lists all installed packages along with their dependencies like a tree structure.

 

10. outdated:

It is used to evaluate whether the installed package’s dependencies are outdated or not. Run this command with -g –depth=0 to evaluate the installed package.

 

11. publish:

This command is used when we are developing our own packages for npm and we need to publish our package to the npm registry.

 

12. search:

Obviously, it is used to search for packages in the npm registry.

 

13. star/unstar:

This command provides the user with an option to give a star to the package of liking, directly from the terminal. npm also has an “unstar” command to revert the star.

 

14. update:

It is used to download and update outdated packages.

 

15. version:

This gives the shorthand version of plugins in package.json.

 

The above mentioned are the subcommands used to manage or configure your npm packages.

npm Configurations:

Configuration is the most important part in the Node Package Manager. We have three ways to configure npm.

  1. Via Terminal
  2. Via npmrc file
  3. Via package.json file

1. Via Terminal:

We can set/edit configuration variables and environment variables via terminal using npm config subcommand. It goes like:

 

We can set the configuration values (which are optional). So, if we do not provide any value it will take the default value. For example, if you have want to access the package in public we should publish the package using –access=public. If we do not provide a value it will take the default value private.
Similarly, we can set or get or delete configurations.

2. Via npmrc file:

We can also set the configuration using .npmrc configuration files. Using this we can set the configuration in different levels based on our requirements.

  1. Project Level – For project level configuration, the file will go along with package.json on root path/to/project/.npmrc.
  2. User Level – The configuration file will go into the home folder of specific user user configured ~/.npmrc.
  3. Global level – Configuration should be saved into root etc folder /etc/.npmrc
  4. Built-in level – This level of configuration not only makes the configuration as global but also makes it as part for the Node Package Manager source code. So it will affect all packages already installed or going to be installed.

.npmrc file configuration can be set or modified by running npm config set <key> <value> command on terminal. Refer this link for more info. Configuration of the value will be save as below

 

3. Via package.json

Finally, we can also use package.json file to configure Node Package Manager. It is a json file, and so we have to write the file in json format and all values should have a name & value pair. For example

 

We have a lot of predefined configuration and environment variables. Like, access, always-auth, color, depth, dry-run, git-tag-version, production, loglevel, etc. For more information, please refer this link.

Conclusion:

This article provides some overview about the Node Package Manager commands and configuration. Although the Node Package Manager has more features than what we have explained in this article, this article is very useful to start with, and get the basics of npm. We will be back with another interesting topic. Happy coding…
Read 10 Useful Unix Commands for every Web Developers here….

The following two tabs change content below.

Manikandan Thangaraj

A Passionate full stack developer with 9 years of experience in full stack web application development and expertise in technologies like PHP and Angular. He has huge craze in learning new things about technologies and constantly shares his knowledge with others.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call