Get started with the basic Node.js & npm commands

May 22, 2015 14:37 GMT  ·  By
npm is a package manager for Node.js and various other types of JavaScript packages
3 photos
   npm is a package manager for Node.js and various other types of JavaScript packages

All Node.js installations come with npm or the Node.js Package Manager, a utility for easily downloading, installing, updating, and removing Node.js libraries, modules, or entire applications. The npm utility is just like APT for some *NIX systems and works exclusively from Node's command-line interface.

Besides being a Node CLI tool, npm is also an online registry where developers can publish their JavaScript libraries, and from where other developers can download the JS files and check to see for recent updates. npm's home and central repository is npmjs.com, and it currently hosts not only Node.js modules, but jQuery plugins as well, since February 2015 to be more exactly.

If you're starting out, there are a few npm commands that are crucial, and you should be well aware of. First is:

code
npm install <package name>
Using this command tells Node.js to download that specific package from the npm servers, install it locally and allow the user to load the library inside his application's code.

This command is regularly used only when developing JavaScript applications, and after the code is finished, the package's name is added to a package.json file as a requirement for the library to work for other developers, without actually packaging the library along with the app's source.

When someone else downloads the application, they can simply access the folder via the command-line and run “npm install” without any other parameters. This makes npm read the package.json file, download all required packages (minimum versions supported) and install them locally so the application can function as normal.

There's also another form of npm install, which is:

code
npm install <package name> -g
The “-g” parameter tells npm to install that specific package globally, allowing developers to use it from anywhere on their filesystem, not just in its installation folder or the applications it is required in.

The opposite of this command is:

code
npm uninstall <package name>
It does exactly what you think it does. The -g parameter can be used as well, to remove globally installed packages.

Updating packages is also a trivial task via:

code
npm update <package name>
Just like “npm install”, navigating to a project and running “npm update” automatically updates all package.json dependencies to their latest versions, and is considered the simplest way to update Node.js-powered forums, CMSs, blogs, and other types of complex applications.
The npm index can also be searched via the Node CLI
The npm index can also be searched via the Node CLI

Similar to the APT in Linux, npm also comes with a search function to allow developers to locate the packages they like, that also features a very simplistic and easy-to-remember syntax:

code
npm search <term>
These are the absolute basic commands you need to get a grip of before going anywhere with Node.js, and if you ever worked with a modern Linux operating system or PHP's Composer, they should be quite simple to get used to.

The basics of using npm in Node.js (3 Images)

npm is a package manager for Node.js and various other types of JavaScript packages
npm can be used to install, update, and remove JavaScript packages from Node.jsThe npm index can also be searched via the Node CLI
Open gallery