Node.js Package Manager - npm
Updated: Oct 6, 2022
The npm is the standard package manager for Node.js, which is used to download and manage dependencies of Node.js packages. The npm registry hosts thousands of packages, which makes it the largest single language code registry. There is a package for almost everything in npm registry
In this article, we will be discussing about the Node.js Packager manager - npm, the theory required to understand it, followed by its installation process and updating steps. By the end of this article, we will be able to install a package for our project and use it in our code with the help of npm.
To understand what is a package, you can refer to the following link, where packages in Node.js are explained in details - Packages And Modules In Node.js
Quick Links -
COMPONENTS OF NPM
The npm consists of three distinct components. They are –
1. Website - It is used to discover packages, set up profiles and manage other aspects of npm.
2. CLI - It runs on a terminal to interact with npm. Most developers use CLI to interact with the npm. Yarn and pnpm are the alternatives to npm cli.
USES OF NPM
As we have seen, the npm registry hosts thousands of packages, which makes it the largest single language code registry. There is a package for almost everything in npm registry. Now, let us try to understand the use of package manager for node.js.
According to the official documentation of npm, some of its major uses are –
1. Package adaptation - npm adapts packages of code for our apps. The code adapted from packages are used in our projects and apps for ease.
2. Availability of standalone tools - It downloads tools which can be used right away.
3. Use of npx - npm uses npx which directly run packages without downloading them in our system.
4. Share ability of code - npm can share code of packages with any npm user at and from anywhere.
5. Code restriction - npm restricts code to specific developers only.
6. Version management - It manages multiple versions of code and code dependencies simultaneously.
7. Discovery of optimized solution - It discovers multiple ways to solve the same puzzle to find optimized solution.
INSTALLATION OF NPM
The npm program gets installed in our system automatically when we install Node.js.
To know how to install Node.js, you can refer to the following link – Introduction to Node.js and Its Installation
Installing all dependencies using npm
NPM creates a folder named “Node_modules”. All the packages that we install for our project are placed here.
The following command –
Installs everything that our project needs in a folder named Node_modules.
Installing a single package
To install a single package, we need to tell npm about the name of the package to be installed. For it, we run the following command –
npm install <package-name>
Suppose we want to install a package named upper-case, we run –
npm install upper-case
Some of the flags used with these commands are –
--Save-Dev -> It installs and adds the entry to the Package.json file DevDependencies [SHORTHAND: -D]
--No-Save -> It installs the entry but does not save it to Package.json file dependencies
--Save-Optional -> It installs and saves the entry in Package.json file OptionalDependencies [SHORTHAND: -O]
--No-Optional -> It prevents optional dependencies from being installed
Updating all packages
By running following command, npm updates all the packages to its newest version which satisfies the versioning constraints of our system.
Updating a single package
To update a single package, we need to specify the name of the package. We run following command to update a specified package –
npm update <Package-Name>
HOW TO USE A PACKAGE INSTALLED USING NPM
As we have seen above, we install packages in a folder named Node_modules. To use the package installed, we need to import it in our code using “require”.
npm install lodash
Now, if we want to use this package in our code, we import it by typing –
const _ = require(‘lodash’);
where, _ is name of the variable. It can be named anything by satisfying the rules for declaring variable names.
At the end, we come to know that npm is the default package manager of Node.js, which we use to download and manage the packages required for our project.
Hope you enjoyed the content and understood the concepts as well.