At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

, , ,

Guide to use the Node Package Manager (npm)

  • By Manikandan Thangaraj
  • February 27, 2017
  • 2014 Views

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.

npm install <package-name>

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

npm install <package-name>@1.#.#.

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

npm install -g mocha angular-cli

 
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:

npm install
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <tarball file>
npm install <tarball url>
npm install <folder>

 

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:

npm uninstall [<@scope>/]<package>[@<version>]...
[-S|--save|-D|--save-dev|-O|--save-optional]

 

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:

npm access public [<package>]
npm access restricted [<package>]
npm access grant <read-only|read-write> <scope:team> [<package>]
npm access revoke <scope:team> [<package>]
npm access ls-packages [<user>|<scope>|<scope:team>]
npm access ls-collaborators [<package> [<user>]]
npm access edit [<package>]

 

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:

npm bin [-g|--global]

 

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:

npm cache add <tarball file>
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version>
npm cache ls [<path>]
npm cache clean [<path>]

 

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:

npm config set <key> <value> [-g|--global]
npm config get <key>
npm config delete <key>
npm config list
npm config edit
npm get <key>
npm set <key> <value> [-g|--global]

 

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.

npm dedupe
npm ddp

 

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.

npm link (in package dir)
npm link [<@scope>/]<package>[@<version>]

 

9. ls:

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

npm ls [[<@scope>/]<package> ...]

 

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.

npm outdated [[<@scope>/]<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.

npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]

 

12. search:

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

npm search [-l|--long] [search terms ...]

 

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.

npm star [<package>...]
npm unstar [<package>...]

 

14. update:

It is used to download and update outdated packages.

 npm update [-g] [<package>...]

 

15. version:

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

npm version

 
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:

npm config --<configuration-option> [<optional-value>].

 
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

access=private
prefix = ${HOME}/.npmpackages
key[] = "first value"
key[] = "second value"

 

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

{
"url" : "https://github.com/project/issues",
"email" : "projectowner@hostname.com"
}

 
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….

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.