Complete guide to setting up and working with Node.js in your development environment.
node --version # Check Node.js version
npm --version # Check npm version
# Create project directory
mkdir my-node-project
cd my-node-project
# Initialize npm project
npm init -y
This creates package.json with default settings.
# Initialize new project
npm init # Interactive setup
npm init -y # Use defaults
# Install all dependencies
npm install
# Install specific package
npm install express # Latest version
npm install express@4.18.2 # Specific version
# Install as development dependency
npm install --save-dev webpack
# Install globally
npm install -g nodemon
# List all installed packages
npm list
# List outdated packages
npm outdated
# Update specific package
npm update express
# Update all packages
npm update
# Remove package
npm uninstall express
# Remove and update package.json
npm uninstall --save express
# Run start script
npm start
# Run custom script
npm run dev
npm run build
npm run test
# List available scripts
npm run
# Run script with arguments
npm run dev -- --port 3001
npm install express
@"
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
// Routes
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/api/data', (req, res) => {
res.json({ message: 'API Response', data: [] });
});
app.post('/api/data', (req, res) => {
const newData = req.body;
res.status(201).json({ success: true, data: newData });
});
// Error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal Server Error' });
});
// Start server
app.listen(PORT, () => {
console.log(\`Server running on http://localhost:\${PORT}\`);
});
"@ | Out-File -Encoding UTF8 server.js
# Run server
node server.js
Visit http://localhost:3000
# Install nodemon globally
npm install -g nodemon
# Or install as dev dependency
npm install --save-dev nodemon
# Run with nodemon
nodemon server.js
# Update package.json
# "scripts": { "start": "nodemon server.js" }
npm start
npm install express # Web framework
npm install fastify # Fast web framework
npm install hapi # Enterprise framework
npm install mongoose # MongoDB ODM
npm install sequelize # SQL ORM
npm install typeorm # TypeORM
npm install prisma # Modern ORM
npm install pg # PostgreSQL client
npm install dotenv # Environment variables
npm install axios # HTTP client
npm install lodash # Utility library
npm install moment # Date/time
npm install uuid # Generate UUIDs
npm install --save-dev nodemon # Auto-reload
npm install --save-dev eslint # Linting
npm install --save-dev prettier # Formatting
npm install --save-dev jest # Testing
npm install --save-dev webpack # Bundling
npm install --save-dev typescript
npm install --save-dev @types/node
npx tsc --init
@"
interface User {
id: number;
name: string;
email: string;
}
function greetUser(user: User): string {
return \`Hello, \${user.name}!\`;
}
const user: User = {
id: 1,
name: 'John',
email: 'john@example.com'
};
console.log(greetUser(user));
"@ | Out-File -Encoding UTF8 main.ts
# Compile to JavaScript
npx tsc main.ts
# Run compiled JavaScript
node main.js
my-app/
├── src/
│ ├── index.js # Entry point
│ ├── server.js # Express setup
│ ├── routes/ # Route handlers
│ │ ├── users.js
│ │ └── products.js
│ ├── controllers/ # Business logic
│ │ ├── userController.js
│ │ └── productController.js
│ ├── models/ # Database models
│ │ ├── User.js
│ │ └── Product.js
│ ├── middleware/ # Custom middleware
│ │ ├── auth.js
│ │ └── validation.js
│ ├── utils/ # Helper functions
│ │ ├── logger.js
│ │ └── validators.js
│ └── config/ # Configuration
│ └── database.js
├── tests/ # Test files
│ ├── unit/
│ └── integration/
├── public/ # Static files
│ ├── css/
│ ├── js/
│ └── images/
├── .env # Environment variables
├── .env.example # Example environment
├── .gitignore # Git ignore rules
├── package.json # Project metadata
├── package-lock.json # Dependency lock
└── README.md # Project documentation
@"
NODE_ENV=development
PORT=3000
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
MONGODB_URL=mongodb://localhost:27017/dbname
API_KEY=your-api-key
SECRET_KEY=your-secret-key
"@ | Out-File .env
# Don't commit .env to git
echo ".env" >> .gitignore
npm install dotenv
# In your code:
# require('dotenv').config();
# const PORT = process.env.PORT || 3000;
npm install --save-dev jest
# Initialize Jest config
npx jest --init
# Or add to package.json
# "test": "jest"
@"
function sum(a, b) {
return a + b;
}
module.exports = sum;
"@ | Out-File math.js
@"
const sum = require('./math');
describe('sum function', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
test('adds 5 + 5 to equal 10', () => {
expect(sum(5, 5)).toBe(10);
});
});
"@ | Out-File math.test.js
# Run tests
npm test
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/server.js"
}
]
}
Then press F5 to start debugging.
console.log('Regular log');
console.error('Error message');
console.warn('Warning message');
console.table([{id: 1, name: 'John'}]);
console.time('label');
// ... code to measure ...
console.timeEnd('label');
# Clear npm cache
npm cache clean --force
# Reinstall dependencies
rm -r node_modules package-lock.json
npm install
# Find process using port
netstat -ano | findstr :3000
# Kill process (replace PID)
taskkill /PID <PID> /F
# Or use different port
NODE_ENV=development PORT=3001 node server.js
# This usually doesn't happen on Windows
# If it does, reinstall Node.js with admin rights
^ for flexibility, ~ for stability