node_02 - Getting Started with Node¶
Three ways to install Node and two of them are wrong for production
You can download the official installer from nodejs.org , use your package manager (apt , brew , yum) , or use nvm (Node Version Manager). The first two lock you into a single Node version managed by someone else's release schedule. NVM lets you switch versions per-project , which is non-negotiable when you maintain multiple apps on different Node releases
installation - do it right¶
nvm (recommended) - installs per-user , switches versions per-shell , doesn't touch system packages
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
# Restart terminal or source your profile
source ~/.bashrc
# Install the latest LTS version
nvm install --lts
# Use it
nvm use --lts
# Set as default
nvm alias default 'lts/*'
Package manager - convenient but lags behind releases
# Ubuntu/Debian
sudo apt update && sudo apt install nodejs npm
# macOS (Homebrew)
brew install node
Official installer - fine for desktops , annoying for automation
Download the LTS version from nodejs.org and run the installer. It adds node and npm to your PATH globally
verify the install¶
node --version
# v22.14.0 (or whatever LTS is current in your timeline)
npm --version
# v10.x.x
If these don't return version numbers , something went wrong. Check your PATH variable and make sure the nvm directory is sourced in your shell profile
write and run your first script¶
Create a file called app.js with this content:
// app.js - your first Node script
console.log('Hello from Node.js')
console.log('Running version:' , process.version)
console.log('Platform:' , process.platform)
Run it:
node app.js
# Hello from Node.js
# Running version: v22.14.0
# Platform: linux
That's it. That's the whole pipeline. Node reads the file , V8 compiles it , and executes it. No HTML page needed , no browser required
the REPL¶
Type node in your terminal with no arguments and you drop into a Read-Eval-Print Loop. It's a live JavaScript playground where you can test code before putting it in a file
$ node
> console.log('testing in repl')
testing in repl
undefined
> const x = 5 + 3
undefined
> x
8
> .exit
Type .exit or press Ctrl-D twice to leave. The REPL is great for testing small snippets but terrible for anything that needs to persist - closes your session and everything is gone
hello world as an HTTP server¶
The classic Node hello world is an HTTP server. This is the minimum viable web server:
// hello-world.js
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req , res) => {
res.statusCode = 200
res.setHeader('Content-Type' , 'text/plain')
res.end('Hello World\n')
})
server.listen(port , hostname , () => {
console.log(`Server running at http://${hostname}:${port}/`)
})
Run it with node hello-world.js and open http://127.0.0.1:3000 in your browser. You'll see "Hello World". Ctrl-C stops the server
Security note: This server has no routing , no input validation , no rate limiting , no CORS handling , no security headers. It's a teaching example , not a production deployment. Every request hits the same callback and gets the same response. Real servers need more
common pitfalls¶
Port already in use - EADDRINUSE means something is already listening on port 3000. Use a different port or kill the other process
# Find what's on port 3000
lsof -i :3000
# Kill it
kill -9 <PID>
Wrong file path - Node throws MODULE_NOT_FOUND or ENOENT if the file doesn't exist. Use absolute paths or verify the relative path from your current working directory
# Check current directory
pwd
# List files
ls
# Run with explicit path
node ./app.js
Permissions - if you installed Node globally with a package manager , you might need sudo for global npm installs. NVM installs to your home directory and avoids this entirely
prerequisites¶
next -> node_03_js_requirements.md