npm & package.json
๐ Covered in this chapter:
npm init ยท npm install ยท Dependencies vs devDependencies ยท Semantic Versioning ยท npm scripts ยท npx
Master npm โ Node's package manager โ including installing dependencies, semantic versioning, npm scripts, and npx.
1npm & package.json โ What You'll Learn
Master npm โ Node's package manager โ including installing dependencies, semantic versioning, npm scripts, and npx.
Here's everything this chapter covers, in the order you'll learn it:
- What npm is
- Creating package.json with npm init
- Installing packages
- Uninstalling packages
- Development dependencies vs production dependencies
- Package versioning
- Semantic versioning (major.minor.patch)
- package-lock.json's role
- npm scripts for common tasks
- npx to run packages without installing globally
- Updating and auditing packages
- Publishing packages (overview)
2Working Example
๐ป Example: npm & package.json
Terminal
โถ Run in Compiler
npm init -y
npm install express
npm install --save-dev nodemon
npm run dev
๐ป Continued Example
JSON
โถ Run in Compiler
{
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js"
}
}
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- Dependencies (--save, default) are needed to run your app in production; devDependencies (--save-dev) are only needed while developing (like testing tools).
- Semantic versioning: MAJOR.MINOR.PATCH โ a bump in MAJOR usually means breaking changes.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about npm & package.json?
Focus on: npm init ยท npm install ยท Dependencies vs devDependencies ยท Semantic Versioning ยท npm scripts ยท npx. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for npm & package.json?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.