Node.js Project Setup from Scratch | npm init, install & run dev | Beginner Tutorial
Video: Node.js Project Setup from Scratch | npm init, install & run dev | Beginner Tutorial by Taught by Celeste AI - AI Coding Coach
Watch full page →Node.js Project Setup from Scratch | npm init, install & run dev
Setting up a Node.js project is straightforward with npm, the Node Package Manager. This guide walks you through initializing a new project, installing Express as a dependency, creating a basic server, and running it in development mode using npm scripts.
Code
// Step 1: Initialize a new Node.js project with default settings
// Run in terminal:
// npm init -y
// Step 2: Install Express framework
// Run in terminal:
// npm install express
// Step 3: Create index.js with a simple Express server
const express = require('express');
const app = express();
const port = 3000;
// Define a route handler for the root URL
app.get('/', (req, res) => {
res.send('Hello, Node.js project setup!');
});
// Start the server and listen on port 3000
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
// Step 4: Add a "dev" script to package.json for running the server with nodemon
// In package.json, add under "scripts":
// "dev": "nodemon index.js"
// Then run the development server with:
// npm run dev
Key Points
npm init -yquickly creates a package.json file with default project settings.npm install expressadds the Express framework and saves it as a project dependency.- A simple Express server listens on port 3000 and responds with a greeting message.
- Adding a
devscript in package.json allows easy starting of the server withnpm run dev. - Using tools like nodemon in the dev script enables automatic server restarts during development.