Monday, January 29, 2018

How To Manage NodeJS Packages Using Npm

https://www.ostechnix.com/manage-nodejs-packages-using-npm

A while ago, we have published a guide to manage Python packages using PIP.
Today, we are going to discuss how to manage NodeJS packages using Npm.
NPM is the largest software registry that contains over 600,000
packages. Everyday, developers across the world shares and downloads
packages through npm. In this guide, I will explain the the basics of
working with npm, such as installing packages(locally and globally),
installing certain version of a package, updating, removing and managing
NodeJS packages and so on.

Manage NodeJS Packages Using Npm

Installing NPM

Since npm is written in NodeJS, we need to install NodeJS in order to
use npm. To install NodeJS on different Linux distributions, refer the
following link.

Once installed, ensure that NodeJS and NPM have been properly installed. There are couple ways to do this.
To check where node has been installed:

$ which node
/home/sk/.nvm/versions/node/v9.4.0/bin/node

Check its version:

$ node -v
v9.4.0

Log in to Node REPL session:

$ node
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
> .exit

Check where npm installed:

$ which npm
/home/sk/.nvm/versions/node/v9.4.0/bin/npm

And the version:

$ npm -v
5.6.0

Great! Node and NPM have been installed and are working! As you may
have noticed, I have installed NodeJS and NPM in my $HOME directory to
avoid permission issues while installing modules globally. This is the
recommended method by NodeJS team.
Well, let us go ahead to see managing NodeJS modules (or packages) using npm.

Installing NodeJS modules

NodeJS modules can either be installed locally or globally(system
wide). Now I am going to show how to install a package locally.Install packages locally
To manage packages locally, we normally use package.json file.
First, let us create our project directory.

You will see the dependencies have been added. The caret (^)
at the front of the version number indicates that when installing, npm
will pull the highest version of the package it can find.

$ ls node_modules/
commander

The advantage of package.json file is if you had the package.json
file in your project’s directory, you can just type “npm install”, then
npm will look into the dependencies that listed in the file and download
all of them. You can even share it with other developers or push into
your GitHub repository, so when they type “npm install”, they will get
all the same packages that you have.
You may also noticed another json file named package-lock.json. This file ensures that the dependencies remain the same on all systems the project is installed on.
To use the installed package in your program, create a file index.js(or any name of you choice) in the project’s directory with the actual code, and then run it using command:

$ node index.js

Install packages globally

If you want to use a package as a command line tool, then it is
better to install it globally. This way, it works no matter which
directory is your current directory.

$ npm install async -g
+ async@2.6.0
added 2 packages in 4.695s

Or,

$ npm install async --global

To install a specific version of a package, we do:

$ npm install async@2.6.0 --global

Updating NodeJS modules

To update the local packages, go the the project’s directory where the package.json is located and run:

$ npm update

Then, run the following command to ensure all packages were updated.

$ npm outdated

If there is no update, then it returns nothing.
To find out which global packages need to be updated, run:

$ npm outdated -g --depth=0

If there is no output, then all packages are updated.
To update the a single global package, run:

$ npm update -g

To update all global packages, run:

$ npm update -g

Listing NodeJS modules

To list the local packages, go the project’s directory and run:

$ npm list
demo@1.0.0 /home/sk/demo
└── commander@2.13.0

As you see, I have installed “commander” package in local mode.
To list global packages, run this command from any location:

Searching NodeJS modules

This command will display all modules that contains the search string “request”.

Removing NodeJS modules

To remove a local package, go to the project’s directory and run following command to remove the package from your node_modules directory:

$ npm uninstall

To remove it from the dependencies in package.json file, use the save flag like below:

$ npm uninstall --save

To remove the globally installed packages, run:

$ npm uninstall -g

Cleaning NPM cache

By default, NPM keeps the copy of a installed package in the cache
folder named npm in your $HOME directory when installing it. So, you can
install it next time without having to download again.
To view the cached modules:

$ ls ~/.npm

The cache folder gets flooded with all old packages over time. It is better to clean the cache from time to time.
As of npm@5, the npm cache self-heals from corruption issues and data
extracted from the cache is guaranteed to be valid. If you want to make
sure everything is consistent, run:

And, that’s all for now. What we have just covered here is just the
basics. NPM is a vast topic. For more details, head over to the the NPM Getting Started guide.
Hope this was useful. More good stuffs to come. Stay tuned!
Cheers!