Package Management with npm
Understand npm, package.json, and how JavaScript projects are organized — the foundation for every Node.js project.
What you'll learn
- Initialize a new project with npm init
- Install and use packages
- Understand package.json and node_modules
- Run scripts defined in package.json
Playwright AutomationlessonsJump to another lesson
Package Management with npm
The Shape of a Node.js Project
Every Node.js project looks like this:
my-test-project
4 fieldsWhat is a Package?
Some packages you’ll meet:
| Package | What it does |
|---|---|
| playwright | Automates browsers for testing |
| vitest | Runs tests |
| chalk | Colors terminal text |
| express | Builds web servers |
| react | Builds user interfaces |
Over 2 million packages are available on npmjs.com.
Starting a New Project
- 1
Create a folder
&& runs both commands in sequence.
mkdir my-first-project && cd my-first-project - 2
Initialize
Creates package.json. The -y flag accepts all defaults — skip the Q&A.
npm init -y - 3
Check what you got
You should see one file: package.json.
ls
$ mkdir my-first-project && cd my-first-project$ npm init -yWrote to /Users/yourname/my-first-project/package.json$ lspackage.jsonTry it in your terminal!
Understanding package.json
{
"name": "my-first-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
package.json
5 fieldsQuick check
What does 'npm init -y' do?
Installing Packages
$ npm install chalkadded 1 package in 2s# chalk makes terminal text colorfulTry it in your terminal!
npm install X
Downloads package
from npmjs.com
Added to package.json
in dependencies
Code in node_modules
ready to import
Dev dependencies
$ npm install --save-dev vitestadded 60 packages in 5s# --save-dev (or -D) marks it as dev-onlyTry it in your terminal!
Using a package
$ touch index.js# In index.js, write:import chalk from 'chalk';console.log(chalk.green('Success!'));console.log(chalk.red('Uh oh!'));# Add "type": "module" to package.json for import syntax$ node index.jsSuccess! (green)Uh oh! (red)Try it in your terminal!
node_modules — The Big Folder
npm Scripts
Scripts are shortcuts you define in package.json:
"scripts": {
"start": "node index.js",
"test": "vitest",
"greet": "echo Hello from npm!"
}
# Special scripts — no "run" needed$ npm start$ npm test# Everything else needs "run"$ npm run greetHello from npm!Try it in your terminal!
| Script name | How you run it |
|---|---|
start | npm start (special) |
test | npm test (special) |
Anything else (e.g. lint, build) | npm run <name> |
Quick check
Which flag marks a package as a dev-only dependency?
Removing Packages
$ npm uninstall chalk# Removes from node_modules, package.json, and package-lock.jsonTry it in your terminal!
Cheat Sheet
| Command | What it does |
|---|---|
npm init -y | Create new project |
npm install | Install everything in package.json |
npm install <pkg> | Install a runtime dependency |
npm install -D <pkg> | Install a dev dependency |
npm uninstall <pkg> | Remove a package |
npm run <script> | Run a custom script |
npm start / npm test | Run special scripts |
npm list | List installed packages |
npm outdated | Check for updates |
The Real-World Workflow
- 1
Clone a project
Download the code (we'll cover git later).
- 2
Install dependencies
Reads package.json, downloads everything into node_modules.
npm install - 3
Run it
Or npm test — whichever the project uses.
npm start - 4
Add more packages as you go
npm install <pkg>
Practice Exercises
Fill in the missing parts of this package.json file:
{ "name": "my-qa-project", "version": "1.0.0", "": "module", "scripts": { "": "node index.js", "test": "vitest" }, "dependencies": {}, "": { "vitest": "^1.6.0" } }
To install Playwright as a dev dependency, type
Put these steps in the correct order to start a new Node.js project and install a testing tool:
- 1npm install --save-dev playwright
- 2npm init -y
- 3cd my-project
- 4mkdir my-project