npm and package.json: Install Packages, Scripts and Versions
npm (Node Package Manager) is installed together with Node.js. It downloads the libraries your project uses, such as Playwright, ESLint and dayjs, and records them in package.json, the file that describes your project. Every JavaScript automation project starts with these two. In this lesson you will create a package.json, install and remove packages, understand version ranges and the lock file, run tasks with npm scripts, and learn the commands a CI pipeline uses. All terminal output below was captured from real runs with Node.js 26 and npm 11.
📖 What You’ll Learn: what npm and package.json are, npm init, installing and uninstalling packages, node_modules, dependencies vs devDependencies, semantic versioning with ^ and ~, package-lock.json, npm install vs npm ci, npm scripts and npm run, npx, global installs, npm audit and the commands you need for CI.
📦 What Are npm and package.json?
Definition: npm is the package manager for JavaScript: a command-line tool plus the public registry at npmjs.com with millions of packages. package.json is a JSON file in the root of your project that lists its name, version, scripts and the packages it depends on.
Check that both are installed:
⌨️ Terminal
node -v
npm -v
🖥️ Output
v26.13.0
11.20.0
| Term | Meaning |
|---|---|
| Package | A folder of reusable code with its own package.json, published to the registry |
| Dependency | A package your project needs, listed in package.json |
node_modules/ | The folder where npm puts installed packages (never edit it, never commit it) |
package-lock.json | The exact version of every installed package, so every machine installs the same tree |
| Registry | The server npm downloads packages from (registry.npmjs.org by default) |
🆕 Creating package.json with npm init
Run npm init in an empty project folder. It asks a few questions; -y accepts the defaults:
⌨️ Terminal
mkdir demo-npm
cd demo-npm
npm init -y
🖥️ Output
Wrote to /home/you/demo-npm/package.json:
{
"name": "demo-npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
}
| Field | Purpose |
|---|---|
name, version | Identify the package (required only if you publish it) |
main | The entry file when someone requires your package |
scripts | Named commands you run with npm run <name> |
type | "commonjs" or "module": how .js files are loaded (see the modules lesson) |
dependencies | Packages needed to run the code |
devDependencies | Packages needed only to develop and test it |
💡 Tip: For a test automation project, change "type" to "module" if you want to use import/export in plain .js files. Recent npm versions write "type": "commonjs" by default.
⬇️ Installing and Removing Packages
npm install <name> (short: npm i) downloads the package into node_modules and adds it to dependencies:
⌨️ Terminal
npm install dayjs
🖥️ Output
added 1 package, and audited 2 packages in 3s
found 0 vulnerabilities
Tools that are only used while developing or testing, such as linters, formatters and test runners, go into devDependencies with -D (--save-dev). Several packages can be installed in one command:
⌨️ Terminal
npm install -D eslint @eslint/js globals prettier
🖥️ Output
added 81 packages, and audited 83 packages in 7s
19 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Four packages became 81 because each package has its own dependencies. npm ls --depth=0 shows only the ones you installed directly:
⌨️ Terminal
npm ls --depth=0
🖥️ Output
demo-npm@1.0.0 /home/you/demo-npm
+-- @eslint/js@10.0.1
+-- dayjs@1.11.23
+-- eslint@10.15.0
+-- globals@17.14.0
`-- prettier@3.10.1
package.json now records both groups:
📄 package.json (excerpt)
{
"dependencies": {
"dayjs": "^1.11.23"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"eslint": "^10.15.0",
"globals": "^17.14.0",
"prettier": "^3.10.1"
}
}
| Command | What it does |
|---|---|
npm install | Install everything listed in package.json (after cloning a project) |
npm install pkg | Add a runtime dependency |
npm install -D pkg | Add a dev dependency |
npm install pkg@1.2.3 | Install an exact version |
npm uninstall pkg | Remove a package and its package.json entry |
npm update | Update packages within the allowed version ranges |
npm outdated | List packages that have newer versions |
npm ls | Show the installed dependency tree |
⚠️ Watch out: Add node_modules/ to .gitignore. It can contain thousands of files and is rebuilt from package.json and package-lock.json with one command, so it never belongs in Git.
🔢 Versions: Semantic Versioning, ^ and ~
npm packages use semantic versioning: MAJOR.MINOR.PATCH, for example 1.11.23.
| Part | Changes when… | Safe to update? |
|---|---|---|
| MAJOR (1.x.x → 2.0.0) | Breaking changes | No, read the release notes first |
| MINOR (1.11 → 1.12) | New features, backwards compatible | Usually |
| PATCH (1.11.23 → 1.11.24) | Bug fixes only | Yes |
The symbol in front of a version in package.json is a range: which future versions npm install and npm update may pick.
| Range | Allows | Example: 1.11.23 allows |
|---|---|---|
^1.11.23 (npm default) | Same major | 1.11.23 up to, but not including, 2.0.0 |
~1.11.23 | Same major and minor | 1.11.23 up to, but not including, 1.12.0 |
1.11.23 | Exactly this version | Only 1.11.23 |
* or latest | Anything | Avoid in real projects |
Your code can read package.json like any JSON file, which is useful for printing versions in a test report:
📄 package.json
{
"name": "qa-framework",
"version": "2.3.0",
"devDependencies": {
"@playwright/test": "^1.50.0",
"eslint": "^10.15.0",
"prettier": "~3.10.1"
}
}
📄 versions.js
const pkg = require("./package.json");
console.log(`${pkg.name} v${pkg.version}`);
for (const [name, range] of Object.entries(pkg.devDependencies)) {
console.log(`dev: ${name.padEnd(12)} ${range}`);
}
⌨️ Terminal
node versions.js
🖥️ Output
qa-framework v2.3.0
dev: @playwright/test ^1.50.0
dev: eslint ^10.15.0
dev: prettier ~3.10.1
🔒 package-lock.json, npm install and npm ci
A range such as ^1.11.23 could install a different version next month. package-lock.json records the exact version of every package in the tree (including dependencies of dependencies), so a teammate or a CI machine installs the identical set.
- Commit package-lock.json to Git together with package.json.
npm installinstalls from package.json and the lock file, and updates the lock file when you add or change packages.npm ci(“clean install”) deletesnode_modulesand installs exactly what the lock file says. It fails if package.json and the lock file disagree. Use it in CI pipelines.
npm install | npm ci | |
|---|---|---|
| Use on | Your machine, while developing | CI servers and fresh checkouts |
| Changes the lock file | Can | Never |
| Needs a lock file | No | Yes |
| Existing node_modules | Reused | Deleted first |
💡 Tip: A typical CI job for a Playwright project runs npm ci, then npx playwright install --with-deps, then npx playwright test.
▶️ npm Scripts
The scripts section gives short names to commands. Inside a script, binaries installed in node_modules/.bin (such as eslint and prettier) can be called directly, without a path. You can edit package.json by hand or use npm pkg set:
⌨️ Terminal
npm pkg set scripts.start="node src/report.js" scripts.lint="eslint ." scripts.format="prettier --write ." "scripts.test=node --test"
📄 package.json (scripts)
"scripts": {
"test": "node --test",
"start": "node src/report.js",
"lint": "eslint .",
"format": "prettier --write ."
}
📄 src/report.js
const dayjs = require("dayjs");
console.log("Report generated on", dayjs("2026-11-23").format("DD MMM YYYY"));
start and test have shortcuts (npm start, npm test); every other script runs with npm run <name>. npm prints the command before running it:
⌨️ Terminal
npm start
🖥️ Output
> demo-npm@1.0.0 start
> node src/report.js
Report generated on 23 Nov 2026
⌨️ Terminal
npm run
🖥️ Output
Lifecycle scripts included in demo-npm@1.0.0:
test
node --test
start
node src/report.js
available via `npm run`:
lint
eslint .
format
prettier --write .
- Pass extra arguments after
--:npm test -- --test-name-pattern=login. - A script named
prelintorpostlintruns automatically before or afterlint. - Common test-project scripts:
"test": "playwright test","test:smoke": "playwright test --grep @smoke","report": "playwright show-report".
❌ What Happens When a Package Is Missing
Remove dayjs and run the script again. The code still requires it, so Node.js cannot find the module:
⌨️ Terminal
npm uninstall dayjs
🖥️ Output
removed 1 package, and audited 82 packages in 2s
19 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
📄 report.js
const dayjs = require("dayjs");
console.log("Report generated on", dayjs("2026-11-23").format("DD MMM YYYY"));
⌨️ Terminal
node report.js
🖥️ Output
node:internal/modules/cjs/loader:1607
throw err;
^
Error: Cannot find module 'dayjs'
Require stack:
- /home/you/js-course/report.js
at Object.<anonymous> (/home/you/js-course/report.js:1:15)
{
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/you/js-course/report.js'
]
}
Node.js v26.13.0
Cannot find module 'dayjs' after cloning a project almost always means you forgot npm install (or npm ci). If it happens for one package only, it is missing from package.json, so install it with npm install dayjs.
🛠️ npx, Global Installs and npm audit
npx: run a package binary
npx <command> runs a binary from the project’s node_modules/.bin, or downloads it temporarily if it is not installed. That is how you run npx playwright test, npx eslint src or npx prettier --check . without writing a script first.
Global installs
npm install -g pkg installs a command for the whole machine. Prefer local devDependencies plus npx for project tools, because every teammate and CI machine then uses the same version.
Security checks
npm audit checks installed packages against a database of known vulnerabilities (every install also runs it, hence “found 0 vulnerabilities”). npm audit fix applies compatible upgrades.
🧪 Tester Scenario: Setting Up a Test Project from Scratch
These are the commands to create a new automation project, and the ones a teammate or CI runs after cloning it:
⌨️ Terminal
# create the project (once)
mkdir qa-framework && cd qa-framework
npm init -y
npm pkg set type=module
npm install -D @playwright/test eslint prettier
npx playwright install
# a teammate after git clone
npm install
npm test
# CI pipeline
npm ci
npx playwright install --with-deps
npm test
📄 .gitignore
node_modules/
test-results/
playwright-report/
.env
Commit package.json, package-lock.json and your source code; ignore node_modules, reports and secrets.
📝 Practice Questions with Answers
Try each question on your own first, then open the answer to check your work. Run the code with node practice.js.
Q1. Which command creates a package.json with default values, and which command adds prettier as a dev dependency?
Show answer
npm init -y creates package.json. npm install -D prettier (or --save-dev) installs Prettier and lists it under devDependencies.
Q2. Write a script that reads package.json and prints how many runtime and dev dependencies it has.
Show answer
📄 package.json
{
"name": "shop-tests",
"dependencies": { "dayjs": "^1.11.23" },
"devDependencies": { "@playwright/test": "^1.50.0", "eslint": "^10.15.0", "prettier": "^3.10.1" }
}
📄 count.js
const pkg = require("./package.json");
const count = (deps) => Object.keys(deps ?? {}).length;
console.log(`dependencies: ${count(pkg.dependencies)}, devDependencies: ${count(pkg.devDependencies)}`);🖥️ Output
dependencies: 1, devDependencies: 3Q3. package.json says "eslint": "^10.15.0". Which of these versions can npm update install: 10.15.4, 10.22.0, 11.0.0?
Show answer
^ allows any version with the same major number, so 10.15.4 and 10.22.0 are allowed and 11.0.0 is not. With ~10.15.0 only 10.15.x (10.15.4) would be allowed.
Q4. Add scripts so that npm run lint runs ESLint on the project and npm run test:smoke runs playwright test --grep @smoke.
Show answer
"scripts": {
"lint": "eslint .",
"test:smoke": "playwright test --grep @smoke"
}Or from the terminal: npm pkg set scripts.lint="eslint ." "scripts.test:smoke=playwright test --grep @smoke".
Q5. Your CI build installs a different version of a library than your laptop. What two things fix this?
Show answer
Commit package-lock.json to Git, and use npm ci in the pipeline instead of npm install. npm ci installs exactly the versions recorded in the lock file and fails if package.json and the lock file disagree.
Q6. A teammate cloned the repo and gets Error: Cannot find module '@playwright/test'. What is the most likely cause and fix?
Show answer
They have not installed the dependencies: node_modules is not in Git. Running npm install (or npm ci) in the project folder installs everything from package.json and the lock file.
🧠 MCQ Quiz: npm and package.json
10 questions on this lesson. Pick one answer for each, then press Check answers to see your score and an explanation for every question.
- What does
npm init -ydo?Answer: Creates package.json with default values. -y accepts every default answer. - Where should ESLint, Prettier and Playwright usually be listed?Answer: devDependencies. They are needed to develop and test, not to run the product.
- Which flag saves a package as a dev dependency?Answer: -D. -D is short for –save-dev.
- What does
^2.4.1allow?Answer: 2.4.1 up to, but not including, 3.0.0. The caret keeps the major version. - Should node_modules be committed to Git?Answer: No, add it to .gitignore. It is rebuilt with npm install or npm ci.
- Why commit package-lock.json?Answer: So every machine installs exactly the same versions. Ranges in package.json alone can resolve differently over time.
- Which command is best for CI pipelines?Answer: npm ci. It is a clean, exact install from the lock file.
- How do you run a script named “lint”?Answer: npm run lint. Only start and test (and a few others) have shortcuts.
- What does npx do?Answer: Runs a package binary, from node_modules/.bin or a temporary download. Example: npx playwright test.
- What does “Cannot find module ‘x'” right after cloning a project usually mean?Answer: Dependencies were not installed yet. Run npm install or npm ci.
💼 Interview Questions and Answers
Q1. What is the difference between dependencies and devDependencies?
dependencies are needed when the code runs in production, for example a date library used by the app. devDependencies are only needed for development, building and testing: test runners, linters, formatters and type definitions. npm install --omit=dev (used for production images) skips devDependencies. In a pure test-automation repository most packages, including Playwright, are devDependencies.
Q2. What is package-lock.json and why does it matter?
It records the exact resolved version, download URL and integrity hash of every package in the dependency tree. Committing it makes installs reproducible: a teammate, a CI machine and your laptop all get the same versions, so a test does not fail on CI just because a transitive dependency released a new version. npm ci installs strictly from it.
Q3. Explain semantic versioning and the ^ and ~ ranges.
Versions are MAJOR.MINOR.PATCH: major for breaking changes, minor for compatible features, patch for fixes. ^1.4.2 accepts any 1.x.x from 1.4.2 (same major), ~1.4.2 accepts 1.4.x from 1.4.2 (same minor), and 1.4.2 pins one exact version. The lock file still decides what is actually installed until you update it.
Q4. npm install vs npm ci?
npm install is for development: it can add packages and update the lock file. npm ci is for automation: it removes node_modules, installs exactly what package-lock.json lists, never writes to package.json or the lock file, and fails if they are out of sync. It is faster and reproducible, so CI pipelines use it.
❓ Frequently Asked Questions
🤔 Is npm installed with Node.js?
Yes. The Node.js installer includes npm and npx. Check with npm -v. Other package managers such as pnpm and Yarn use the same package.json.
🤔 What is the difference between npm and npx?
npm installs and manages packages and runs scripts. npx runs a command from a package, using the locally installed version when there is one.
🤔 How do I update a package to a new major version?
Read its release notes for breaking changes, then install the new major explicitly, for example npm install -D eslint@11, and run your tests and linting.
🤔 Can I delete node_modules?
Yes, at any time. npm install or npm ci recreates it from package.json and package-lock.json. Deleting it is a common fix for strange install problems.
🏁 Summary
- npm installs packages from the registry; package.json describes the project, its scripts and its dependencies.
npm install pkgadds a runtime dependency,npm install -D pkgadds a dev dependency, andnpm uninstall pkgremoves one.- Versions are MAJOR.MINOR.PATCH;
^keeps the major,~keeps the minor. - Commit package.json and package-lock.json, ignore node_modules, and use
npm ciin CI. - Name repeated commands in
scriptsand run them withnpm run; use npx to run package binaries.
Next, continue with Lesson 54: File System and Path.
🎬 Watch JavaScript and automation tutorials on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in