It is almost impossible to manually resolve and manage packages for your Node.js projects. This npm tutorial for beginners will show you how to use this package manager to get it done.
What Is npm?
The purpose of npm (the online repository) is to help developers publish their Node.js packages so others can reuse them in their own applications. Thanks to the utility of the same name, they can easily install, update, and uninstall these packages in their local system through the command-line interface.
Plenty of Node.js applications and libraries are newly uploaded or updated in the npm registry every day. Using npm to take advantage of those packages is a must-have skill for any Node.js developer.
The npm program should already come with your Node.js installation. You can verify its existence with this command:
When npm has been installed and is accessible to your terminal app, it should print the installed version:
If the output returns an error indicating the shell can’t find the npm command, you should install nvm (Node Version Manager) first and then use it to install Node.js and npm.
On Linux and macOS, run one of these two commands to install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
On Windows, get the nvm-set.exe file from this repository, and run it to install nvm.
Open a new terminal window, then check the version of your nvm installation:
Use this command to install the stable version of Node.js:
nvm install stable
Verify our Node.js installation:
How npm Works
The package.json file is the heart of any Node.js application, including Node.js modules or packages. This JSON file is located in the root directory of a project, containing its metadata, such as its name, description, and dependencies.
Package managers like npm need these pieces of information to identify the package and manage its dependencies. Without the package.json file, npm wouldn’t be able to get every package required to run your Node.js project, start it, run other scripts, or publish it to the registry.
As you will learn below, you can create and manage the package.json file of your project with npm as well.
npm also maintains a lockfile named package-lock.json in the same place as package.json. While package.json provides the high-level overview of your project, package-lock.json is where npm keeps the snapshots of its dependency tree.
You should never edit this file manually. It works with the package.json file to keep track of your project’s dependencies.
When you ask npm to install certain packages or resolve your project’s dependencies, they will be placed in the directory named “node_modules” at the root of your project.
In particular, npm will read and update package.json and package-lock.json if needed, download and install the packages into node_modules, and finally, run an audit of these packages to find security vulnerabilities.
npm Tutorial For Beginners
Initialize A Node.js Project
You can use the npm init command to create a package.json in a brand-new project. It should be located in an empty directory, where every part of your project is saved locally.
Switch the current working directory to your project, and run this command:
It will ask you several questions about your project, such as its name, author, and version. All the fields are optional, and you can just hit Enter to use the default values npm gives you.
By default, npm will use “npm” as the project name, “1.0.0” as its version, “index.js” as the main script, and “ISC” as the software license. Enter the
Here is how simple a package.json file may look:
"name" : "learnshareit",
"description" : "npm tutorial for beginners",
"version" : "0.0.1",
This file sets “learnshareit” as the name of the project and “express” as its sole dependency.
At this point, npm still hasn’t installed any package to your project.
Note: if you want to generate a package.json file without npm asking any questions, run this command:
npm init -v
After creating a package.json file with your project’s required packages in it, you can run the npm install command with no arguments to install all those dependencies to the node_modules directory:
By default, npm will parse the package.json file and find the packages listed in the dependency field. With the example above, npm will install the Express framework, all of its dependencies, dependencies of these dependencies, and so on, from the npm registry.
Output like this means the install process has run without errors:
added 57 packages, and audited 58 packages in 6s
found 0 vulnerabilities
npm also creates the package-lock.json to save the current state of the dependency tree.
While developing your Node.js project, you can always install packages from the npm registry when your project needs them.
For example, use this command to install the Axios library:
npm install axios
By default, npm install enables the –save option. This means the above package will also be added as a dependency to the package.json file.
Set this option to false if you don’t want npm to automatically update package.json:
npm install --save=false axios
Run this command to update all the packages listed in package.json (and their dependencies) to the latest version:
This operation also installs any missing packages.
The npm uninstall command will remove a package and its dependencies from your project:
npm uninstall axios
npm will also remove the package from the package.json file.
- Missing script: “start” npm error
- Error “yarn: command not found”
- Webpack: command not found error
- Create-next-app: command not found error
- Firebase: command not found error
- nx: command not found error
- Lerna: command not found error
The package manager npm can help you install dependencies for your Node.js projects. This npm tutorial for beginners has shown you the basic operations for a new project. Follow other guides of ours to learn more about npm and its role in Node.js projects.
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Name of the university: HUST