Node Package Manager(npm)
Let’s learn how to use the Node Package Manager (npm).
We'll cover the following
The npm
package is the standard package manager for Node.js. This package is also used in the frontend to manage project dependencies.
How do we use npm?
First, we initialize the project and create a basic package.json
file, like this:
npm init
If a package.json
file already exists, we can install all the project dependencies by running this code:
npm install
To install a single package, we use the following code:
npm install <package_name>
To install a single package globally, we use the following code:
npm install <package_name> -g
To update installed packages, we use the following code:
npm update
To update a single installed package, we use the following code:
npm update
To run a task, we use the following code:
npm run <task_name>
The package.json
file
The package.json
file is created by the npm
package when initializing a project. We can think of the package.json
file as a project manifest that contains all the project settings. It’s where we can store names and different versions of all the installed packages.
Here’s a typical package.json file generated by the npm
package:
{
"name": "Educative Nodejs Full-Stack Course",
"version": "0.1.0",
"description": "A full-stack application built using React, Nodejs, Express, and MongoDB",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^14.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/aashrafh/educative-Nodejs-fullstack-course"
},
"keywords": [
"node",
"express"
],
"author": "Ahmed Ashraf",
"contributors": [
"Ahmed Ashraf <ahmed.ashraf.cmp@gmail.com> (https://github.com/aashrafh)"
],
"license": "MIT"
}
The package.json
file is a typical JSON object that has a set of key-value pairs. Each property below indicates an important part of the project setting that’s used by the npm
package or other tools:
-
The
version
indicates the current version of the project. -
The
description
consists of a few words about the application. -
The
main
property sets the entry point of the project. -
The
scripts
are a set of node scripts that we can run. -
The
dependencies
set a list of all the installednpm
packages. -
The
repository
is the location of the application or package. -
The
keywords
array describes what our application 0r package does. -
The
author
property is the name of the author. -
The
contributors
array lists all the contributors. -
This
license
property is the license of the application or package.
The packages-lock.json
file
The packages-lock.json
file is automatically generated by the npm
package, and we, most probably, won’t need to edit or modify it. This file is used to track the exact version of each installed package so that the application can be reproduced with 100% accuracy, even if the package is updated by its maintainer.
Get hands-on with 1400+ tech skills courses.