Skip to main content

How to Install an npm Package

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

npm, the package manager that comes with Node.js, installs other people's code into your project with one command. npm install downloads the package, records it in package.json and makes it ready to import.

Syntax​

Syntax
npm install <package>[@<version>]
PartWhat it does
npm installDownloads a package into the project in the folder you're in and records it as a dependency.
<package>The package's name on the npm registry, such as nanoid.
@<version>Optional. A version or range, such as @6.0.2. Without it, npm installs the newest version.

The Example​

The example builds a small project, id-maker, that prints a random ID with nanoid, a package that generates short unique IDs, the kind used in links and database keys.

Prerequisites​

  • Node.js, which comes with npm. Check it with npm --version. This guide uses npm 12.2.3 and Node.js 26.

Step 1: Create the Project​

Terminal
mkdir id-maker
cd id-maker
npm init -y
npm pkg set type=module

npm init -y creates the project's package.json, and npm pkg set type=module lets its code use import. These commands, like the rest, are the same in zsh, bash and PowerShell.

Step 2: Install the Package​

Terminal
npm install nanoid
Output
added 1 package, and audited 2 packages in 957ms

1 package is looking for funding
run `npm fund` for details

found 0 vulnerabilities

npm added one package and checked the project's packages against its list of known security problems, and found none. npm fund lists packages whose authors ask for donations. It's information only, and you don't need to run it.

Three things changed in the folder:

  • node_modules, a new folder, holds the package's code.
  • package.json now lists it under dependencies.
  • package-lock.json, a new file, records the exact version that was installed, so everyone who installs the project gets the same one.
package.json
{
"name": "id-maker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "module",
"dependencies": {
"nanoid": "^6.0.2"
}
}

^6.0.2 means 6.0.2 or any newer 6.x release. A later npm install on another computer can pick up 6.1.0, but never 7.0.0, which could change how the package works.

Step 3: Use It​

Create main.js:

main.js, which imports nanoid from the nanoid package and prints a new ID.

main.js
import { nanoid } from 'nanoid';

console.log(nanoid());

An import from a bare name such as 'nanoid', with no ./ in front, loads an installed package from node_modules.

Terminal
node main.js
Output
IybdGW0y5e6J00h4m7zao

Your ID will differ: it's random every time.

A zsh terminal where npm pkg set type=module and npm install nanoid add one package with 0 vulnerabilities found, and node main.js prints a random 21-character ID.

Common Mistakes​

MistakeWhat happensDo this instead
Running npm install <package> outside the project foldernpm installs it wherever you arecd into the project first
Committing node_modules to GitThe repository fills with code anyone can download againCommit package.json and package-lock.json, and add node_modules to .gitignore
Deleting package-lock.jsonThe next install may pick different versionsKeep it, and commit it

Troubleshooting​

SymptomLikely causeFix
Cannot find package 'nanoid' when the code runsIt isn't installed in this projectRun npm install nanoid in the project's folder
SyntaxError: Cannot use import statement outside a moduleThe project is still "type": "commonjs"npm pkg set type=module

FAQ​

How do I install every package a project lists?​

Run npm install with no package name in the project's folder. npm reads package.json and package-lock.json and installs exactly those versions.

How do I install an exact version?​

Add it after an @, as in npm install nanoid@6.0.2. How to Pin Exact npm Package Versions shows how to record it without the ^.

Conclusion​

npm install <package> downloads a package into node_modules, adds it to package.json and fixes its exact version in package-lock.json. After that, import loads it by its name.

Additional Resources​