Getting Started with Node.js: Install, Setup, and Hello World

Getting Started with Node.js: Install, Setup, and Hello World

이 글의 핵심

A practical guide to Node.js: install the runtime, configure your environment, run Hello World, use npm, and build a minimal HTTP server with clear examples.

Introduction

What is Node.js?

Node.js is a JavaScript runtime built on Chrome’s V8 engine. It lets you run JavaScript outside the browser.

Key characteristics:

  • Non-blocking I/O: High throughput for I/O-heavy workloads
  • Single-threaded model: Event loop–driven concurrency
  • Cross-platform: Windows, macOS, and Linux
  • npm: The world’s largest package ecosystem
  • One language: Share JavaScript between frontend and backend

Good fits for Node.js:

  • REST and JSON APIs
  • Real-time apps (chat, games, collaboration)
  • Microservices and BFF layers
  • CLI tools and build scripts
  • Streaming and upload pipelines

Poor fits:

  • CPU-heavy work (image/video transcoding, large numerical jobs)
  • Work that needs true parallel CPU crunching (use workers, native addons, or another runtime)

1. Node.js vs browser JavaScript

AspectBrowser JavaScriptNode.js
EnvironmentBrowser (Chrome, Firefox, …)Server, local machine
Globalwindowglobal
DOMYesNo
File systemNoYes (fs)
HTTP serverNo (without bundling/workarounds)Yes (http, https)
ModulesES Modules (modern)CommonJS + ES Modules
Package managerNone built-innpm, yarn, pnpm

Shared JavaScript APIs

console.log("Hello");
setTimeout(() => console.log("After 1s"), 1000);
setInterval(() => console.log("Tick"), 1000);

const fetchData = async () => {
    const result = await Promise.resolve("data");
    return result;
};

const obj = JSON.parse('{"name": "Alice"}');
const str = JSON.stringify({ name: "Alice" });

Node-only APIs

const fs = require('fs');
const content = fs.readFileSync('file.txt', 'utf8');

const http = require('http');
const server = http.createServer((req, res) => res.end('Hello'));

const path = require('path');
const filePath = path.join(__dirname, 'file.txt');

console.log(process.version, process.platform, process.cwd(), process.pid);

Prefer async fs methods on servers; sync calls block the event loop.


2. Installing Node.js

Windows

  1. nodejs.org → download LTS
  2. Run the installer
  3. Verify: node --version, npm --version

macOS

Official installer or brew install node.

Linux (Ubuntu/Debian example)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 20
nvm use 20

3. First programs

Hello World

console.log("Hello, Node.js!");
console.log(`Node ${process.version}, platform ${process.platform}`);

CLI arguments

console.log(process.argv);
const args = process.argv.slice(2);
if (args.length === 0) {
    console.log("Usage: node args.js <name>");
    process.exit(1);
}
console.log(`Hello, ${args[0]}!`);

Environment variables

const port = process.env.PORT || 3000;
const nodeEnv = process.env.NODE_ENV || 'development';
console.log(port, nodeEnv);

4. First HTTP server

const http = require('http');

const server = http.createServer((req, res) => {
    console.log(`${req.method} ${req.url}`);
    res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
    res.end('Hello from Node.js!');
});

const PORT = 3000;
server.listen(PORT, () => console.log(`http://localhost:${PORT}`));

Add routing by branching on req.url and req.method; return JSON with JSON.stringify and Content-Type: application/json.


5. npm

npm init -y
npm install express
npm install --save-dev nodemon
{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
}

6. Modules

CommonJS: module.exports / require. ES modules: "type": "module" in package.json, then import / export. Same patterns as the modules article.


7. File system (fs)

Sync (readFileSync), callback (readFile), and Promise (require('fs').promises) styles exist—use Promises + async/await for server code.


8. Examples

Minimal multi-route server, static file server, and CLI directory counter—see the filesystem guide for full listings.


9. nodemon

npm install --save-dev nodemon
{ "scripts": { "dev": "nodemon server.js" } }

10. Debugging

VS Code launch.json with "type": "node", or node inspect script.js.


11. dotenv

npm install dotenv

Load secrets from .env; never commit .env (list in .gitignore).


12. Common issues

  • EADDRINUSE — port busy; pick another port or kill the process
  • Cannot find module — run npm install
  • Wrong path — use path.join(__dirname, ...)
  • Async mistakes — use await / .catch() consistently

13. Tips

Typical layout: src/ (routes, controllers, utils), public/, tests/, .env, package.json. Handle uncaughtException, unhandledRejection, and graceful shutdown on SIGTERM. Use streams for large files; consider cluster for CPU-bound scaling.


Summary

Node.js is the V8-based JavaScript runtime for servers and tools. Install LTS, use npm, prefer async fs and HTTP APIs for services, and move on to modules and Express.

Next steps

Resources