Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

npm
node.js
communitycreator

How to use the node package manager

Abel Lifaefi Mbula

If you decide to migrate to JavaScript as a backend for your next application, the only option is to use Node.js. The software we build always relies on other software, and the node package manager (npm) lets you easily manage dependencies within your app. You can compare npm to composer in PHP.

The purpose of this shot is to teach you how to use npm effectively.

Prerequisites: Make sure you already have Node.js installed on your system.

npm in a nutshell

npm is a dependencies’ manager tool. More precisely, npm is a CLICommand Line Interface client that comes with Node.js by default. So, if you have Node.js installed on your system, you also have npm.

Note: To ensure you have Node.js installed on your system, type node --version in a shell or terminal window.

To work with npm, you need to prepare your workspace.

  • Create a project directory: mkdir myProject
  • Make it a Node.js project: npm init

The second command will prompt you with some questions that you’ll need to answer.

npm init
npm init

Once everything is OK, you type yes and hit the Enter button, and npm will create a package.json file with all the information.

Note: If you want to use the default answers from npm init, you can type npm init -y.

Basic npm commands

You can use the commands below in a terminal:

  • npm -v or npm — version: gets the npm version
  • npm install -g npm: updates npm to the latest release
  • npm i <packageName> or npm install <packageName>: installs a package
  • npm uninstall <packageName>: uninstalls package packageName
  • npm outdated: checks for an updated version of a package
  • npm update <packageName>: updates packageName
  • npm search <name>: searches for a package
  • npm cache clean — force: cleans the cache
  • npm audit: audits packages for known security vulnerabilities
  • npm ls: shows installed packages
  • npm help: gets help

Install packages

Now, we will install dependencies in our project.

First, make sure you have a clean and ready Node.js workspace.

The package we want to install is called express, a Node.js framework.

npm i express

npm will fetch express from npmjs.com, which is the default registry it uses. A registry is a public database of JavaScript software.

After the package is downloaded, npm places it in a special directory called node_modules, and the reference of the module is written in the package.js file under the dependencies section. You can view the package by entering ls in the terminal.

node_modules: The default directory used by Node.js to find any package it needs. Think of this folder as a local store for your downloaded packages.

Our recently installed express module is shown below:

The package.json and package-lock.json files

The reference for the express package was written in the package.json file. This is the case for any package you install, which is important because it makes it easy for you or someone else to get started with your project in a new instance (be it a computer or another directory).

Additionally, when working with git, you’ll need to use .gitignore on the node_modules folder because it is so big and does not belong to you (or your project codebase). The only way to reinstall all packages used in the project will be via package.json.

Delete node_modules and run npm install. The directory will be created again, and all referenced modules in package.json will be installed.

The package-lock.json file helps lock the same version used for your project dependencies. This file guarantees that they have the same version you used when someone is installing your project dependencies.

Summary

npm is a JavaScript package manager for Node.js. To make a Node.js project, you have to use npm init.

npm install <moduleName> is used to install packages. All installed packages are stored in the node_modules directory and referenced in package.json under the dependencies section. package-lock.json will lock the version of the dependencies you use in the project.

Happy coding!

RELATED TAGS

npm
node.js
communitycreator
RELATED COURSES

View all Courses

Keep Exploring