Package Management with npm

Understand npm, package.json, and how JavaScript projects are organized — the foundation for every Node.js project.

Playwright Automation Prep Lesson 3
14 min read

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

Package Management with npm

The Shape of a Node.js Project

Every Node.js project looks like this:

my-test-project

4 fields
package.json:"recipe card"
node_modules/:"toolbox of code"
package-lock.json:"exact versions"
your tests:"the actual work"
Every Node.js project has this structure. Once you know it, every project feels the same.

What is a Package?

Some packages you’ll meet:

PackageWhat it does
playwrightAutomates browsers for testing
vitestRuns tests
chalkColors terminal text
expressBuilds web servers
reactBuilds user interfaces

Over 2 million packages are available on npmjs.com.

Starting a New Project

  1. 1

    Create a folder

    && runs both commands in sequence.

    mkdir my-first-project && cd my-first-project
  2. 2

    Initialize

    Creates package.json. The -y flag accepts all defaults — skip the Q&A.

    npm init -y
  3. 3

    Check what you got

    You should see one file: package.json.

    ls
npm init in action
$ mkdir my-first-project && cd my-first-project$ npm init -yWrote to /Users/yourname/my-first-project/package.json$ lspackage.json

Try 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 fields
name:"my-project"
version:"1.0.0"
scripts:"{ test: "vitest" }"
dependencies:"{ chalk: "^5.0" }"
devDependencies:"{ vitest: "^1.6" }"
The highlighted fields are the ones you'll edit most. The rest is mostly metadata.

Quick check

Question 1 of 10 correct

What does 'npm init -y' do?

Installing Packages

Install a regular dependency
$ npm install chalkadded 1 package in 2s# chalk makes terminal text colorful

Try 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

Install a dev-only package
$ npm install --save-dev vitestadded 60 packages in 5s# --save-dev (or -D) marks it as dev-only

Try it in your terminal!

Using a package

Try chalk in a file
$ 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!"
}
Running npm scripts
# Special scripts — no "run" needed$ npm start$ npm test# Everything else needs "run"$ npm run greetHello from npm!

Try it in your terminal!

Script nameHow you run it
startnpm start (special)
testnpm test (special)
Anything else (e.g. lint, build)npm run <name>

Quick check

Question 1 of 10 correct

Which flag marks a package as a dev-only dependency?

Removing Packages

Uninstall
$ npm uninstall chalk# Removes from node_modules, package.json, and package-lock.json

Try it in your terminal!

Cheat Sheet

CommandWhat it does
npm init -yCreate new project
npm installInstall 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 testRun special scripts
npm listList installed packages
npm outdatedCheck for updates

The Real-World Workflow

  1. 1

    Clone a project

    Download the code (we'll cover git later).

  2. 2

    Install dependencies

    Reads package.json, downloads everything into node_modules.

    npm install
  3. 3

    Run it

    Or npm test — whichever the project uses.

    npm start
  4. 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:

  1. 1npm install --save-dev playwright
  2. 2npm init -y
  3. 3cd my-project
  4. 4mkdir my-project

Check Your Understanding

Question 1 of 30 correct

What does package.json do in a Node.js project?