fs.readFile(path, callback) | Read file async |
fs.readFileSync(path) | Read file sync |
fs.writeFile(path, data, callback) | Write file async |
fs.appendFile(path, data, callback) | Append to file |
fs.unlink(path, callback) | Delete file |
fs.mkdir(path, callback) | Create directory |
fs.readdir(path, callback) | Read directory |
fs.stat(path, callback) | Get file stats |
import { readFile } from 'fs/promises' | Import promise API |
await readFile(path, "utf8") | Read file |
await writeFile(path, data) | Write file |
await mkdir(path, { recursive: true }) | Create nested dirs |
await rm(path, { recursive: true }) | Remove recursively |
await access(path) | Check file exists |
path.join(dir, file) | Join paths |
path.resolve(path) | Absolute path |
path.dirname(filepath) | Directory name |
path.basename(filepath) | File name |
path.extname(filepath) | File extension |
path.parse(filepath) | Parse path object |
__dirname | Current directory (CJS) |
import.meta.dirname | Current directory (ESM) |
http.createServer((req, res) => {}) | Create server |
server.listen(port, callback) | Start listening |
req.method | HTTP method |
req.url | Request URL |
req.headers | Request headers |
res.statusCode = 200 | Set status code |
res.setHeader(name, value) | Set header |
res.end(body) | Send response |
await fetch(url) | GET request |
await fetch(url, { method: "POST" }) | POST request |
body: JSON.stringify(data) | JSON body |
headers: { "Content-Type": "application/json" } | Set headers |
const data = await response.json() | Parse JSON |
const text = await response.text() | Get text |
response.ok | Check success |
response.status | Status code |
new URL(urlString) | Parse URL |
url.pathname | URL path |
url.searchParams | Query params |
url.searchParams.get("key") | Get param |
new URLSearchParams(queryString) | Parse query string |
params.toString() | Stringify params |
const emitter = new EventEmitter() | Create emitter |
emitter.on("event", callback) | Listen to event |
emitter.once("event", callback) | Listen once |
emitter.emit("event", data) | Emit event |
emitter.off("event", callback) | Remove listener |
emitter.removeAllListeners() | Remove all |
fs.createReadStream(path) | Read stream |
fs.createWriteStream(path) | Write stream |
readStream.pipe(writeStream) | Pipe streams |
stream.on("data", chunk => {}) | Data event |
stream.on("end", () => {}) | End event |
stream.on("error", err => {}) | Error event |
const { pipeline } = require("stream/promises") | Pipeline utility |
setTimeout(callback, ms) | Delay execution |
setInterval(callback, ms) | Repeat execution |
setImmediate(callback) | Next iteration |
process.nextTick(callback) | Before I/O |
clearTimeout(id) | Clear timeout |
await setTimeout(ms) | Await timer (timers/promises) |
process.env.NODE_ENV | Environment variable |
process.argv | Command line args |
process.cwd() | Current directory |
process.exit(code) | Exit process |
process.pid | Process ID |
process.platform | OS platform |
process.memoryUsage() | Memory info |
exec(command, callback) | Execute command |
execSync(command) | Execute sync |
spawn(command, args) | Spawn process |
fork(modulePath) | Fork Node process |
child.stdout.on("data", data => {}) | Read stdout |
child.on("exit", code => {}) | Exit event |
npm init -y | Initialize package.json |
npm install package | Install package |
npm install -D package | Install dev dependency |
npm install -g package | Install globally |
npm uninstall package | Uninstall package |
npm update | Update packages |
npm run script | Run script |
npm list | List packages |
"type": "module" | Enable ESM |
"main": "index.js" | Entry point (CJS) |
"exports": { ".": "./index.js" } | Exports field |
"scripts": { "start": "node app.js" } | NPM scripts |
"engines": { "node": ">=18" } | Node version |
"dependencies": {} | Runtime deps |
"devDependencies": {} | Dev deps |
import pkg from "package" | Default import |
import { named } from "package" | Named import |
import * as pkg from "package" | Namespace import |
import("package") | Dynamic import |
export default value | Default export |
export { named } | Named export |
export { value as alias } | Export with alias |
const pkg = require("package") | Require module |
const { named } = require("package") | Destructure require |
module.exports = value | Export value |
exports.named = value | Named export |
__filename | Current file path |
__dirname | Current dir path |