Node.js Cheat Sheet
Covers CommonJS and ES module syntax, filesystem operations, async patterns, the built-in HTTP server, and core Node.js globals.
3 PagesIntermediateMar 2, 2026
CommonJS & ES Modules
The two module systems Node.js supports.
javascript
// CommonJS (default in .js unless package.json has "type": "module")const fs = require('fs');module.exports = { greet };function greet(name) { return `Hi ${name}`; }// ES Modules (package.json "type": "module", or .mjs extension)import fs from 'fs';export function greet(name) { return `Hi ${name}`; }export default greet;
Filesystem & Async Patterns
Reading files and working with streams and events.
javascript
const fs = require('fs/promises');async function readConfig() { const data = await fs.readFile('config.json', 'utf8'); return JSON.parse(data);}// Streams for large filesconst { createReadStream, createWriteStream } = require('fs');createReadStream('input.txt').pipe(createWriteStream('output.txt'));// EventEmitter patternconst { EventEmitter } = require('events');const emitter = new EventEmitter();emitter.on('data', (chunk) => console.log(chunk));emitter.emit('data', 'hello');
Built-in HTTP Server
Creating a server without a framework.
javascript
const http = require('http');const server = http.createServer((req, res) => { if (req.url === '/health' && req.method === 'GET') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'ok' })); return; } res.writeHead(404); res.end('Not found');});server.listen(3000, () => console.log('Listening on 3000'));
Core Modules & Globals
Frequently used built-ins.
- process.env- access environment variables
- process.argv- command-line arguments passed to the script
- __dirname / __filename- CommonJS-only globals for the current module's directory and file path
- path.join() / path.resolve()- cross-platform path construction
- Buffer- handles raw binary data
- child_process.spawn() / exec()- run external commands from within Node
- event loop phases- timers, pending callbacks, poll, check, and close callbacks, in that order
Pro Tip
Prefer the fs/promises API with async/await over callback-based fs methods: it composes cleanly with try/catch and avoids callback nesting, and Node has shipped it as stable since version 14.
Was this cheat sheet helpful?
Explore Topics
#NodeJs#NodeJsCheatSheet#WebDevelopment#Intermediate#CommonJSESModules#FilesystemAsyncPatterns#BuiltInHTTPServer#CoreModulesGlobals#Networking#Concurrency#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance