Here's a step-by-step guide to starting a Node.js project:
Download and install Node.js from nodejs.org (npm is included).
Verify installation:
node -v
npm -v
Create a project folder and navigate into it:
mkdir my-node-project
cd my-node-project
Initialize the project (creates package.json):
npm init -y # -y skips prompts and uses defaults
Install packages (e.g., Express.js):
npm install express # Example for a web server
Install dev tools (optional):
npm install --save-dev nodemon # Auto-reloads server during development
Create basic files/directories:
my-node-project/
├── node_modules/
├── src/
│ ├── index.js # Main entry file
│ ├── routes/ # API routes (optional)
│ └── public/ # Static files (optional)
├── package.json
└── .gitignore
In src/index.js:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
In package.json, add:
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js" # If using nodemon
}
For production:
npm start
For development (with nodemon):
npm run dev
Initialize Git:
git init
Create .gitignore:
node_modules/
.env
Install dotenv:
npm install dotenv
Create .env file:
PORT=3000
Load in code:
require('dotenv').config();
const port = process.env.PORT;
Build REST APIs with Express.js
Connect to a database (e.g., MongoDB, PostgreSQL)
Add authentication (e.g., JWT, Passport.js)
Write tests with Jest/Mocha
Deploy to platforms like Heroku, AWS, or Vercel
Let me know if you'd like details on any specific part! 🚀