Usually, when developing a Node.js application, you need to use a set of external tools and libraries to speed up the development process. However, manually managing all the dependencies in projects is very problematic. Fortunately, there is a special tool called npm that can install and manage the packages you need.
In this topic, we will study the basics of working with npm, what parts it consists of, and how it can be used for different purposes.
What is npm
npm is a free tool for JS developers to install and share their packages with developers from all over the world. The name means Node Package Manager because it was initially created as the default package manager for Node.js. This tool is successfully used to manage both open-source and private development.
The npm logo
Using npm you can download new packages, update and delete them, share your own packages with other users (after registering on the official website), and much more.
npm consists of three main components:
a command-line client that is used to download and publish packages;
a remote registry that keeps all public and private packages in one place;
a helpful website where you can discover other packages.
All three are managed by the same organization, npm, Inc.
Installation
If you already have Node.js, you're likely to have npm as well. To check, just invoke the npm -v or npm --version commands in the terminal. You can use it to find out the version of the npm installed:
npm -v
9.5.1If for some reason you don't have npm or you haven't installed Node.js yet, follow this official guide and install them both.
Note that npm -v and npm --version are the same commands. The npm -v is a shorthand command with a single dash -, and npm --version is the long one with a double dash --.
Commands overview
The full list of npm commands is quite long. Here are only a few of the main ones:
npm initto initialize a new project;npm install <package>(ornpm i <package>) to install the package you need. For example, if you want to install the "express" package, the command would be:npm install expressnpm uninstall <package>(ornpm un <package>) to uninstall a package;npm update(ornpm up) to update all packages;npm lsto list all installed packages.
Initialize new package
Let's take a closer look at several commands. To initialize a new project, you should use the npm init command, that starts an installation utility that will answer your questions about your package:
/my-npm-package % npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (my-npm-package)
version: (1.0.0)
description: This is my first npm package
entry point: (index.js)
test command: node test.js
git repository:
keywords: first-project, start, javascript, node
author: Me
license: (ISC)
About to write to /my-npm-package/package.json:
{
"name": "my-npm-package",
"version": "1.0.0",
"description": "This is my first npm package",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"keywords": [
"first-project",
"start",
"javascript",
"node"
],
"author": "Me",
"license": "ISC"
}
Is this OK? (yes)After answering the questions, npm creates the package.json file and prints it to the console, such as in the example above.
You can avoid walking through the installation process by using the npm init -y command. It will just create the package.json file with the minimal required information and return the content of the created package.json file.
Install other packages
To install other packages and add new dependencies to your package, you could use the npm install <package-name> or npm i <package-name> commands. It is quite simple, but you also can control the installation process. For example, you can install some package as the development dependency, which means this package has no effect on the result of your package but can be helpful during the development process. To install some packages as dev-dependency, use the npm i --save-dev <package-name> or npm i -D <package-name> commands. Take a look at the example:
npm install -D nodemonThe result of the code in the example above will create a new Dev-dependency and install nodemon package. nodemon is a tool to restart the node execution after each file saving, but it has no effect on your code and is useless when your application is running in production. After the nodemon installed, you will see the following lines in the package.json file:
"devDependencies": {
"nodemon": "^2.0.22"
}npm i is one of the aliases of the npm install command. To install a package, you can use any alias from the following list: add, i, in, ins, inst, instal, isnt, isnta, isntall.
On the other hand, you can control which version of a package should be installed:
npm i [email protected]You can specify the version of a package via the following construction: npm i <package-name>@<package-version>. It might be useful when you need to install a special version of a package since it is critical for your application and can have an effect on the other modules.
Sometimes you will need to install packages globally to access them from anywhere on your computer. To install some packages globally, type npm i --global <package-name> or npm i -g <package-name>. For example, you want to switch the node version for some reason, so you need to install a package named n:
sudo npm i --global nAfter the installation, you can use the n package to download, install and enable the needed version of Node.js. The following command will switch you to the 16th version of Node.js:
sudo n 16To install and use global packages, you might have to do commands with administration rights!
Conclusion
npm is the default package manager for Node.js developers. It has a CLI client, an online registry, and a website. In this topic, you've learned how to check the version of the npm installed on your device and how to use some other useful commands. Now, let's get down to practice!
Read more on this topic in Setting Up Your First React Project: A Step-by-Step Guide on Hyperskill Blog.