Node.js Cheat Sheet

Node.js is an open-source, cross-platform, server-side JavaScript runtime environment that allows developers to build scalable and high-performance applications. It was created by Ryan Dahl in 2009 and has since become one of the most popular technologies for building web applications.

Node.js is built on top of the V8 JavaScript engine, which is the same engine used by Google Chrome. This means that Node.js is incredibly fast and efficient, making it ideal for building real-time applications that require high performance.

This cheat sheet provides an extensive list of Node.js commands and their descriptions. The commands are organized into different categories for easy reference.

Table of Contents

Basics

CommandDescription
nodeStarts the Node.js REPL
node file.jsRuns the JavaScript file file.js
console.log()Prints a message to the console
process.argvReturns an array of command-line arguments
process.exit()Exits the Node.js process
setTimeout()Executes a function after a specified time
setInterval()Executes a function repeatedly after a specified time
clearTimeout()Cancels a setTimeout() method
clearInterval()Cancels a setInterval() method

File System

CommandDescription
fs.readFile()Reads a file asynchronously
fs.readFileSync()Reads a file synchronously
fs.writeFile()Writes to a file asynchronously
fs.writeFileSync()Writes to a file synchronously
fs.appendFile()Appends to a file asynchronously
fs.appendFileSync()Appends to a file synchronously
fs.rename()Renames a file
fs.unlink()Deletes a file
fs.mkdir()Creates a directory
fs.rmdir()Deletes a directory
fs.readdir()Reads the contents of a directory

HTTP

CommandDescription
http.createServer()Creates an HTTP server
http.request()Sends an HTTP request
http.get()Sends an HTTP GET request
http.post()Sends an HTTP POST request
http.put()Sends an HTTP PUT request
http.delete()Sends an HTTP DELETE request

Events

CommandDescription
events.EventEmitterCreates an event emitter object
emitter.on()Adds a listener function to an event
emitter.emit()Emits an event with the specified arguments
emitter.once()Adds a one-time listener function to an event
emitter.removeListener()Removes a listener function from an event
emitter.removeAllListeners()Removes all listener functions from an event

Streams

CommandDescription
fs.createReadStream()Creates a readable stream
fs.createWriteStream()Creates a writable stream
stream.pipe()Pipes a readable stream to a writable stream
stream.on()Adds a listener function to a stream event
stream.pause()Pauses a readable stream
stream.resume()Resumes a readable stream
stream.destroy()Destroys a stream

Child Processes

CommandDescription
child_process.exec()Executes a command in a shell
child_process.spawn()Spawns a new process
child_process.fork()Spawns a new Node.js process
child_process.kill()Kills a process
process.stdinA readable stream for standard input
process.stdoutA writable stream for standard output
process.stderrA writable stream for standard error

Modules

CommandDescription
module.exportsExports a module
require()Imports a module
__dirnameThe directory name of the current module
__filenameThe file name of the current module
exportsAn object that is used to export a module

Debugging

CommandDescription
console.log()Prints a message to the console
console.error()Prints an error message to the console
debuggerStops the execution of a script and opens the debugger

References