Code Reference

Getting Started with Node.js

Node.js · Reference cheat sheet

Getting Started with Node.js

Node.js · Reference cheat sheet


📋 Overview

Node.js is a JavaScript runtime built on V8 for building servers, CLIs, and tooling outside the browser. Packages are managed with npm, pnpm, or yarn.

🔧 Core concepts

IdeaMeaning
RuntimeExecutes JS with Node APIs (fs, http, process)
ModuleCommonJS (require) or ESM (import) unit
npmDefault package manager + registry client
Event loopSchedules async I/O callbacks
LTSLong-term support release line (prefer for prod)

Check install:

node --version
npm --version
CommandPurpose
node app.jsRun a script
npm init -yCreate package.json
npm install expressAdd dependency
node --watch app.jsRestart on file changes (Node 18+)

💡 Examples

Hello server:

import http from "node:http";

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

server.listen(9000, () => console.log("http://localhost:9000"));

package.json type module:

{
  "name": "demo",
  "type": "module",
  "scripts": { "start": "node index.js" }
}

⚠️ Pitfalls

  • Mixing CJS and ESM without clear "type" / extensions causes cryptic import errors.
  • Global npm install -g tools can conflict across projects — prefer local + npx.
  • Blocking the event loop with heavy sync CPU work starves all requests.

On this page