320 x 50 Mobile Ad
Cheat Sheets

Node.js Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

CLI & Execution

node index.js

Execute a JavaScript file

node -v

Check Node.js version

node -e 'console.log("Hi")'

Evaluate inline script

node --watch index.js

Run with built-in watch mode (Node v18+)

Package Management (NPM)

npm init -y

Initialize a new package.json

npm install <pkg>

Install a package and add to dependencies

npm i -D <pkg>

Install as a development dependency

npm uninstall <pkg>

Remove a package

npm run <script>

Run a script defined in package.json

npx <pkg>

Execute a package binary without installing globally

Globals

__dirname

Directory name of current module (CommonJS)

__filename

File name of current module (CommonJS)

process.cwd()

Current working directory

process.env.PORT

Access environment variables

process.exit(1)

Exit process with failure code

Modules

const fs = require('fs');

Import CommonJS module

module.exports = { a: 1 };

Export CommonJS module

import fs from 'fs';

Import ES module (requires "type": "module" in package.json)

export const a = 1;

Export ES module

File System

const fs = require('fs/promises');

Import Promise-based FS API

await fs.readFile('f.txt', 'utf8')

Read file contents

await fs.writeFile('f.txt', 'data')

Write data to file

await fs.appendFile('f.txt', 'data')

Append data to file

await fs.unlink('f.txt')

Delete a file

await fs.mkdir('./newDir')

Create a directory

await fs.readdir('./dir')

Read directory contents

fs.existsSync('path')

Check if file/dir exists (synchronous)

Path Module

const path = require('path');

Import Path module

path.join(__dirname, 'f.txt')

Join multiple path segments safely

path.resolve('f.txt')

Resolve into an absolute path

path.basename('/foo/bar.txt')

Get the filename ('bar.txt')

path.extname('index.html')

Get file extension ('.html')

HTTP Server

const http = require('http');

Import HTTP module

http.createServer((req, res) => { res.end('Hello'); }).listen(3000);

Create a simple web server

Events

const EventEmitter = require('events'); const myEmitter = new EventEmitter();

Create event emitter instance

myEmitter.on('event', () => {});

Register an event listener

myEmitter.emit('event', arg);

Trigger an event

Child Process

const { exec } = require('child_process');

Import exec from child_process

exec('ls -la', (err, stdout, stderr) => { ... })

Run a shell command

Advanced

Advanced Command Snippet

Explore advanced configurations for Nodejs

System Optimization

Performance tuning best practices for Nodejs

Security Audit

Run security checks and validation for Nodejs

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Nodejs Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Nodejs commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Nodejs and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Nodejs commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Nodejs features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Nodejs reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Nodejs operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online