Node.js Masterclass
High-Performance Backends01.Home02.Introduction03.Environment Setup04.Modules & Exports05.File System (fs)06.Path & OS Modules07.Buffer & Streams08.Events & EventEmitter09.HTTP Module10.NPM & Package.json11.Express.js Fundamentals12.Express Routing13.Express Middleware14.RESTful API Development15.Asynchronous Programming16.Error Handling17.Database with Mongoose18.Authentication with JWT19.Environment Variables20.Testing with Jest21.Deployment & PM2
NPM & Package.json
NPM stands for Node Package Manager. It's the world's largest software registry and comes installed by default with Node.js.
1. Initializing a Project
To start using NPM, you need to create a package.json file. This file acts as the manifest for your project.
# Create package.json interactively
npm init
# Create with default values (Quick)
npm init -y2. Installing Packages
You can install packages locally (for the project) or globally (for your whole system).
# Install locally (Production Dependency)
npm install express
# Install locally as Dev Dependency (Testing/Tools)
npm install nodemon --save-dev
# Install specifically for your machine
npm install -g create-react-app3. Understanding package.json
Key fields in your package.json:
- version: Your application's version.
- scripts: Shortcuts for terminal commands (e.g.,
npm start). - dependencies: Packages required for the app to run.
- devDependencies: Packages only needed during development.
4. NPM Scripts
You can define custom commands in the scripts section.
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "jest"
}Now you can run npm run dev to start your server with nodemon.
Tip: Never modify the
node_modules folder directly. Use NPM commands to add or remove packages to keep your package.json in sync.