Skip to content
Practical SkillsChapter 46 / 47Beginner4 min read

npm & Packages

Learn how npm packages are installed and managed in JavaScript projects, including package.json, node_modules, scripts and version numbers.

On this page

1. What are npm and packages?

A package is a folder with code and a package.json file. npm installs together with Node.js, so if node -v works, npm -v works too.

2. Why does it matter?

Formatting dates, calling an API, or building a React app: writing everything from scratch is a waste of time. With npm you get tested code in a single command.

Without npmWith npm
Download library files by handOne command with npm install
Hard to remember versionsThe version is written in package.json
Everyone on the team has different versionspackage-lock.json gives everyone the same version
Updating is manual worknpm update and npm outdated

3. Where is it used?

JobExample
Creating a projectnpm create vite@latest
Installing a librarynpm install react, npm install zod
Dev toolsESLint, Prettier, TypeScript, Vitest
Running scriptsnpm run dev, npm run build, npm test
Running a tool without installing itnpx prettier --write .

4. How does it work?

npm install flow
npm install zod
The package is downloaded from the npm registry
It is saved in the node_modules folder
The dependency is added to package.json
The exact version is locked in package-lock.json
File / folderJobIn Git?
package.jsonProject name, scripts, dependenciesYes
package-lock.jsonThe exact version of every packageYes
node_modules/The actual downloaded codeNo, it goes in .gitignore

Version numbers

A version has the major.minor.patch format, like 19.2.0.

SymbolMeaning
^19.2.019.2.0 or any newer 19.x.x, the major version will not change
~19.2.0Patch updates only, 19.2.0 or any newer 19.2.x
19.2.0Exactly this version

5. Code example

A new project and packages

bash
mkdir my-app
cd my-app
npm init -y
npm install dayjs
npm install -D prettier
npx prettier --version

package.json

json
{
  "name": "my-app",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "format": "prettier --write ."
  },
  "dependencies": {
    "dayjs": "^1.11.13"
  },
  "devDependencies": {
    "prettier": "^3.6.2"
  }
}

Using a package

js
import dayjs from "dayjs";

console.log(dayjs().format("DD MMM YYYY"));
bash
npm run start
npm run format

How does the code work?

ItemMeaning
npm init -yCreates a default package.json
npm install dayjsA runtime dependency, needed for the app to run
-DA devDependency, needed only during development
npxRuns the command of an installed or remote package
"type": "module"Tells Node that import / export will be used
npm run formatRuns the command written in scripts

Interview tip

People ask the difference between npm install and npm ci. npm ci installs the exact versions from package-lock.json only and deletes node_modules first, so it is better for CI and deployment.

6. Common mistakes

Mistake: Committing node_modules to Git

Why? This folder has thousands of files and can be rebuilt any time with npm install.

Better: Keep node_modules in .gitignore and commit only package.json and package-lock.json.

Mistake: Deleting package-lock.json

Why? Then every developer may get slightly different versions, and you get works on my machine bugs.

Better: Commit the lock file and use npm ci in CI.

Mistake: Installing a package for every small thing

Why? Every package adds security risk and bundle size.

Better: Before installing, check whether built-in JavaScript can do the job, and check the package's downloads and maintenance.

Mistake: Making dev tools a normal dependency

Why? A production install then pulls in packages it does not need.

Better: Install tools like ESLint, Prettier and TypeScript with the -D flag.

7. What to learn before and after

Final quick revision

PointDetail
npmPackage manager and registry
package.jsonProject details, scripts, dependencies
package-lock.jsonExact versions, commit it
node_modulesDownloaded code, do not commit it
-DdevDependency
npxRun a package's command without a global install