NPM (Node Package Manager) or simply npm is a simple application to install node programs. NPM also support a feature which will indicate dependencies and package starting file.
In this article, we will discuss about how to use npm, from a basic command to a rather advance one.
npm has a lot of feature, therefore let’s start by knowing how to get a help. NPM has a lot of help documentation about all of its command. Getting a help is as simple as typing this command on your terminal:
If you have a problem with any of npm command/feature, you can append –help on the command, leading to a documentation specific to that command. For example, both are valid command:
npm help install npm install --help
npm ls [filter]
NPM can see the list of all packages and their version (npm ls with no filter), or filter by a tag (npm filter tag). For examples:
npm ls installed
This will list all installed package.
npm ls stable
This will list all stable package available (installed or not).
Search by Content
npm ls name
This way, we can have all packages that have “name” inside its name or tags.
Search by Version
npm ls @1.0
This is used to query packages specified by its version.
Install a Package
NPM is used to install,update, and remove a package. That’s what a package managers do.
When we want to install something, we can use following command and let NPM install the latest version of it to our machine. Here we will have “pkg_name” installed on our system.
npm install pkg_name
You can also install from a tarball, a folder, or an url to a tarball. If you run npm install without arguments, it tries to install the current folder. For example, we will install express:
npm install express
Or we can also install a specific version of a package by:
npm install [email protected]
We can also install the latest of a package within a version range we specify. For instance:
npm install [email protected]">=0.1.0"
More filters can be used to select a specific packages.
npm update [pkg_name]
The update command does a few things:
- Search the registry for new versions of all the packages installed (if pkg_name is omitted) or specific packages.
- If there’s a newer version, then install it
- Point dependent packages at the new version, if it satisfies their dependency
- Remove old versions, if no other package names them as a dependency.
Basically, update behaves a lot like a “standard” package manager’s update command, except that it also checks to make sure that the new version isn’t going to break anything before it points stuff at it.
This command will uninstall a package. If the version are omitted, then all the found version are removed.
npm rm pkg_name[@version] [pkg_name2[@version]]
Read Package Information
To view all of a package information, we can do like this:
npm view pkg_name[@version]nodejs, tools