Node.js npm & package.json

Install and manage packages.

✍️ 2 exercises in this lesson

npm installs third-party packages and manages your project.

  • npm init -y: create package.json.
  • npm install express: add a dependency (saved in node_modules and listed in package.json).
  • npm install -D nodemon: a development-only dependency.
  • npm uninstall pkg, npm update, npm outdated
  • "scripts" in package.json define commands run with npm run name.
Commit package.json and package-lock.json, but add node_modules/ to .gitignore.

Key Takeaways: Node.js npm & package.json

Mastering Node.js npm & package.json helps you write clean and reliable Node.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Node.js npm & package.json Code Example

Node.js
{
  "name": "my-api",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node server.js",
    "dev": "node --watch server.js"
  },
  "dependencies": {
    "express": "^4.21.0"
  }
}
Output
$ npm run dev
> my-api@1.0.0 dev
> node --watch server.js
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Node.js npm & package.json Practice Exercises

Check what you learned in “Node.js npm & package.json”.

/ 2 solved
Fill in the blank

Install Express as a dependency.

npm  express
✓ Correct! Not quite. Try again.
Multiple choice

Which folder should you NOT commit to Git?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services