Outsourced IT Partner
Support ยท systems ยท strategy
ยฉ 2026
One person, one business โ IT support here, websites at webology.online.
One person, one business โ IT support here, websites at webology.online.
๐ข Node.js Runtime Environment
Quick Reference & Cheat Sheet - Core Modules, APIs & Built-in Functions
Core Modules
File System (fs)
fs.readFile(path, callback)
fs.readFileSync(path)
fs.writeFile(path, data, callback)
fs.writeFileSync(path, data)
fs.appendFile(path, data, callback)
fs.unlink(path, callback)
fs.mkdir(path, callback)
fs.rmdir(path, callback)
fs.readdir(path, callback)
fs.stat(path, callback)
fs.exists(path, callback)
fs.createReadStream(path)
fs.createWriteStream(path)
const fs = require('fs');
// Async file operations
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
// Sync file operations
const data = fs.readFileSync('file.txt', 'utf8');
// Promises (fs.promises)
const fsPromises = require('fs').promises;
async function readFile() {
try {
const data = await fsPromises.readFile('file.txt', 'utf8');
return data;
} catch (error) {
console.error(error);
}
}
HTTP Module
HTTP Server & Client
const http = require('http');
// Create HTTP server
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
// HTTP client request
const options = {
hostname: 'www.example.com',
port: 80,
path: '/api/data',
method: 'GET'
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
});
});
req.end();
Path Module
Path Operations
path.join(...paths)
path.resolve(...paths)
path.dirname(path)
path.basename(path)
path.extname(path)
path.parse(path)
path.format(pathObject)
path.isAbsolute(path)
path.relative(from, to)
path.normalize(path)
const path = require('path');
// Join paths
const fullPath = path.join('/users', 'john', 'documents', 'file.txt');
// Get directory name
const dir = path.dirname('/users/john/file.txt'); // '/users/john'
// Get file name
const file = path.basename('/users/john/file.txt'); // 'file.txt'
// Get extension
const ext = path.extname('/users/john/file.txt'); // '.txt'
Process & Global Objects
Process Object
process.argv
process.env
process.cwd()
process.exit(code)
process.pid
process.platform
process.version
process.uptime()
process.memoryUsage()
process.nextTick(callback)
// Command line arguments
console.log(process.argv);
// Environment variables
console.log(process.env.NODE_ENV);
// Current working directory
console.log(process.cwd());
// Exit process
process.exit(0);
// Process events
process.on('exit', (code) => {
console.log(`Process exited with code ${code}`);
});
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
process.exit(1);
});
Events & Streams
EventEmitter
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
// Add listener
myEmitter.on('event', (data) => {
console.log('Event received:', data);
});
// Emit event
myEmitter.emit('event', 'Hello World');
// Once listener (fires only once)
myEmitter.once('start', () => {
console.log('Started');
});
// Remove listener
const listener = () => console.log('Listener');
myEmitter.on('test', listener);
myEmitter.removeListener('test', listener);
Streams
const fs = require('fs');
// Readable stream
const readStream = fs.createReadStream('input.txt');
readStream.on('data', (chunk) => {
console.log('Received chunk:', chunk);
});
// Writable stream
const writeStream = fs.createWriteStream('output.txt');
writeStream.write('Hello ');
writeStream.write('World!');
writeStream.end();
// Pipe streams
readStream.pipe(writeStream);
// Transform stream
const { Transform } = require('stream');
const upperCaseTransform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
readStream.pipe(upperCaseTransform).pipe(writeStream);
Utilities & Modules
URL Module
const url = require('url');
// Parse URL
const myURL = new URL('https://example.com:8000/path?query=value#hash');
console.log(myURL.hostname); // 'example.com'
console.log(myURL.pathname); // '/path'
console.log(myURL.search); // '?query=value'
// Legacy URL parsing
const parsed = url.parse('https://example.com/path?query=value');
console.log(parsed.host); // 'example.com'
console.log(parsed.pathname); // '/path'
Crypto Module
const crypto = require('crypto');
// Generate hash
const hash = crypto.createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex'));
// Generate random bytes
crypto.randomBytes(16, (err, buffer) => {
if (err) throw err;
console.log(buffer.toString('hex'));
});
// HMAC
const hmac = crypto.createHmac('sha256', 'secret-key');
hmac.update('data to hash');
console.log(hmac.digest('hex'));
OS Module
os.platform()
os.arch()
os.cpus()
os.totalmem()
os.freemem()
os.hostname()
os.uptime()
os.userInfo()
os.homedir()
os.tmpdir()
NPM & Package Management
Package.json
{
"name": "my-app",
"version": "1.0.0",
"description": "My Node.js application",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest"
},
"dependencies": {
"express": "^4.18.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"nodemon": "^2.0.15",
"jest": "^27.5.1"
},
"engines": {
"node": ">=14.0.0"
}
}
NPM Commands
npm init
npm install package
npm install -g package
npm install --save-dev package
npm uninstall package
npm update
npm list
npm run script
npm publish
npm version patch/minor/major