Node.js Fundamentals

Learn Node.js from scratch including core concepts, modules, and basic server development.

beginner Backend Development 5 hours

Chapter 2: Node.js Installation and Setup

Chapter 2 of 15

Chapter 2: Node.js Installation and Setup

2.1 Installation

Installing Node.js is straightforward. Choose the method that best suits your development environment.

Direct Installation:

  • Download from nodejs.org
  • Choose LTS (Long Term Support) version for stability
  • Or Current version for latest features
  • Run installer and follow prompts
  • Includes npm automatically

Version Managers (Recommended):

  • nvm (Node Version Manager): Switch between Node versions easily
  • nvm-windows: Windows version of nvm
  • fnm: Fast Node Manager (cross-platform)
  • Manage multiple Node.js versions
  • Switch versions per project
// nvm commands
nvm install 18.0.0        // Install specific version
nvm use 18.0.0            // Switch to version
nvm list                  // List installed versions
nvm alias default 18.0.0  // Set default version

Package Managers:

  • Homebrew (Mac): brew install node
  • Chocolatey (Windows): choco install nodejs
  • apt (Linux): sudo apt install nodejs npm

2.2 Verifying Installation

Verify Node.js and npm are installed correctly.

// Check Node.js version
node --version
// Output: v18.0.0

// Check npm version
npm --version
// Output: 9.0.0

// Run Node.js REPL (Read-Eval-Print Loop)
node
// Interactive JavaScript environment
// Type .exit to quit

Test Installation:

// Create test file: test.js
console.log('Node.js is working!');

// Run file
node test.js
// Output: Node.js is working!

2.3 Development Environment Setup

Configure your development environment for Node.js development.

Code Editor:

  • VS Code with Node.js extensions
  • Configure ESLint for code quality
  • Set up Prettier for formatting
  • Install Node.js debugging tools

Project Structure:

my-project/
├── src/
│   └── index.js
├── package.json
├── .gitignore
└── README.md

2.4 First Node.js Application

Create your first Node.js application.

// Create app.js
const http = require('http');

const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, Node.js!');
});

server.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

// Run: node app.js