Node.js Overview & Environment Setup
What Node.js is, the V8 engine and Libuv, CommonJS vs ESM, nvm version management, npm and package.json, and writing your first server.
What is Node.js
Node.js runs the V8 JavaScript engine (the same one in Chrome) outside the browser, with Libuv providing the event loop, thread pool and async IO. It lets frontend developers write backends in JavaScript, and its npm registry is the largest package ecosystem in the world.
- Best for: REST APIs, real-time apps (WebSocket, chat), BFF layers, tooling and CLIs.
- Think twice for: CPU-heavy computation (video encoding, ML inference) — it blocks the event loop.
Version Management with nvm
# 安装 nvm 后
nvm install 22 # 安装 LTS
nvm use 22
node -v # v22.x
npm -v
# 国内镜像加速
npm config set registry https://registry.npmmirror.com
npm and package.json
mkdir app && cd app
npm init -y # 生成 package.json
npm install express # 依赖写入 dependencies
npm install --save-dev jest # 开发依赖 devDependencies
npm install # 按 package-lock.json 还原
npm run dev # 执行 scripts.dev
# 锁定语义化版本:
# ^1.2.3 允许 1.x.x
# ~1.2.3 允许 1.2.x
# 1.2.3 精确版本
CommonJS vs ESM
// CommonJS(默认)
const fs = require('fs');
exports.hello = function () { return 'hi'; };
// ESM:package.json 加 "type": "module"
import fs from 'fs';
export function hello() { return 'hi'; }
// 动态 import(两者皆可)
const { hello } = await import('./lib.js');
Modern projects prefer ESM (top-level await, better static analysis); many legacy packages remain CommonJS. Both work in Node 22, but you cannot mix require and import in the same file freely.
Your First Server
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Hello, BackendWiki' }));
});
server.listen(3000, () => console.log('listening on 3000'));
Node.js 是什么
Node.js 把 Chrome 的 V8 引擎搬到浏览器之外运行,由 Libuv 提供事件循环、线程池与异步 IO。前端工程师可以用同一门语言写后端,而 npm 是全球最大的包生态。
- 最适合:REST API、实时应用(WebSocket、聊天)、BFF 层、工具链与 CLI。
- 慎用于:CPU 密集计算(视频编码、ML 推理)——会阻塞事件循环。
用 nvm 管理版本
# 安装 nvm 后
nvm install 22 # 安装 LTS
nvm use 22
node -v # v22.x
npm -v
# 国内镜像加速
npm config set registry https://registry.npmmirror.com
npm 与 package.json
mkdir app && cd app
npm init -y # 生成 package.json
npm install express # 依赖写入 dependencies
npm install --save-dev jest # 开发依赖 devDependencies
npm install # 按 package-lock.json 还原
npm run dev # 执行 scripts.dev
# 锁定语义化版本:
# ^1.2.3 允许 1.x.x
# ~1.2.3 允许 1.2.x
# 1.2.3 精确版本
CommonJS 与 ESM
// CommonJS(默认)
const fs = require('fs');
exports.hello = function () { return 'hi'; };
// ESM:package.json 加 "type": "module"
import fs from 'fs';
export function hello() { return 'hi'; }
// 动态 import(两者皆可)
const { hello } = await import('./lib.js');
新项目优选 ESM(支持顶层 await、静态分析更好);大量旧包仍是 CommonJS。Node 22 两者都支持,但同一文件里不能随意混用 require 与 import。
第一个服务器
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Hello, BackendWiki' }));
});
server.listen(3000, () => console.log('listening on 3000'));