DevAcademy
LearnNode.jsnpm Basics
BeginnerNode.js

npm Basics

Install, manage, and run packages using npm, Node.js's default package manager.

Reading Time

12 min

Lesson

Lesson 4 of 34

What npm Does

npm (Node Package Manager) installs third-party packages from the npm registry, manages their versions, and runs project scripts — it is the backbone of the Node.js ecosystem.

Common npm Commands

npm init -y                # create a package.json
npm install express        # install a dependency
npm install -D typescript  # install a dev-only dependency
npm uninstall express      # remove a dependency
npm run <script>           # run a script defined in package.json

Dependency Types

TypeFlagExample
dependencies(default)express, react — needed to run the app
devDependencies-D or --save-devtypescript, eslint — only needed during development
peerDependenciesn/a — declared in package.jsonA version a plugin expects the host app to already provide

Installing Everything from package.json

npm install
# Reads package.json and installs every listed dependency into node_modules

package-lock.json

npm generates a package-lock.json file recording the exact resolved version of every package (including nested dependencies). Committing this file ensures everyone on a team — and your CI pipeline — installs the exact same versions.

Best Practice

Always commit package-lock.json to version control. It is what makes "works on my machine" dependency issues far less common.

Interview Questions

Quick Quiz

1. What does npm install -D typescript do differently from npm install typescript?

2. What is the purpose of package-lock.json?

3. What does npm install (with no package name) do?