Affiliate link

Node.js - Cheatsheet

Here is a Node.js cheatsheet that covers some of the most commonly used commands and features:

This cheatsheet covers many of the essential commands and features of Node.js. Node.js has a vast ecosystem of modules and tools, so this cheatsheet is by no means exhaustive. Nonetheless, it should provide a useful reference for developers starting with Node.js.

Basic Commands

CommandDescription
node app.jsRun a Node.js application
npm installInstall project dependencies defined in package.json
npm startStart a Node.js application
npm run <script>Run a script defined in the package.json file
node -vCheck the version of Node.js installed
npm -vCheck the version of npm installed

Module Management

CommandDescription
const module = require('module-name')Import a module
module.exports = { ... }Export a module
npm install --save <package>Install a package and add it to the project dependencies
npm install --save-dev <package>Install a package and add it to the project dev dependencies
npm uninstall <package>Uninstall a package
npm lsList installed packages
npm outdatedCheck for outdated packages
npm updateUpdate installed packages
npm auditCheck for known security vulnerabilities

File System Operations

CommandDescription
const fs = require('fs')Import the fs module
fs.readFile('file.txt', (err, data) => { ... })Read a file
fs.writeFile('file.txt', 'Hello World!', (err) => { ... })Write to a file
fs.appendFile('file.txt', 'Hello World!', (err) => { ... })Append to a file
fs.mkdir('directory', (err) => { ... })Create a directory
fs.readdir('directory', (err, files) => { ... })Read the contents of a directory
fs.unlink('file.txt', (err) => { ... })Delete a file
fs.rmdir('directory', (err) => { ... })Delete a directory
fs.createReadStream('file.txt')Create a readable stream
fs.createWriteStream('file.txt')Create a writable stream

HTTP Operations

CommandDescription
const http = require('http')Import the http module
http.createServer((req, res) => { ... })Create a new HTTP server
res.writeHead(200, { 'Content-Type': 'text/plain' })Set the response headers
res.end('Hello World!')Send the response
http.get('http://example.com/', (res) => { ... })Make an HTTP GET request
http.request(options, (res) => { ... })Make an HTTP request with custom options

HTTPS Operations

CommandDescription
const https = require('https')Import the https module
https.createServer(options, (req, res) => { ... })Create a new HTTPS server with custom options
https.get('https://example.com/', (res) => { ... })Make an HTTPS GET request
https.request(options, (res) => { ... })Make an HTTPS request with custom options

Stream Operations

CommandDescription
const stream = require('stream')Import the stream module
const readableStream = new stream.Readable()Create a new readable stream
const writableStream = new stream.Writable()Create a new writable stream
readableStream.push('Hello World!')Push data to a readable stream
readableStream.pipe(writableStream)Pipe data from a readable stream to a writable stream

Event Handling

CommandDescription
const events = require('events')Import the events module
const eventEmitter = new events.EventEmitter()Create a new event emitter
eventEmitter.on('eventName', (arg1, arg2, ...) => { ... })Add a listener for an event
eventEmitter.emit('eventName', arg1, arg2, ...)Emit an event with arguments

Process Management

CommandDescription
process.argvGet the command-line arguments
process.envGet the environment variables
process.exit()Exit the current process
process.on('eventName', () => { ... })Add a listener for a process event
process.chdir('directory')Change the current working directory
process.cwd()Get the current working directory

Asynchronous Control Flow

CommandDescription
setTimeout(() => { ... }, delay)Run a function after a specified delay
setInterval(() => { ... }, interval)Run a function at a specified interval
Promise.resolve(value)Create a resolved Promise
Promise.reject(error)Create a rejected Promise
Promise.all([promise1, promise2, ...])Wait for all Promises to resolve
Promise.race([promise1, promise2, ...])Wait for the first Promise to resolve

Debugging and Profiling

CommandDescription
console.log(value)Log a message to the console
console.error(value)Log an error message to the console
console.time(label)Start a timer
console.timeEnd(label)Stop a timer
debuggerPause the execution and enter the debugger
node --inspect app.jsStart the Node.js debugger
node --prof app.jsGenerate a CPU profiling report

Testing

CommandDescription
const assert = require('assert')Import the assert module
assert.strictEqual(actual, expected)Check if two values are strictly equal
assert.deepStrictEqual(actual, expected)Check if two objects have the same properties and values
npm install --save-dev mochaInstall the Mocha testing framework
npm install --save-dev chaiInstall the Chai assertion library
it('testName', () => { ... })Create a new test case
expect(actual).to.equal(expected)Check if two values are equal using Chai

This cheatsheet covers many of the essential commands and features of Node.js. Keep in mind that Node.js has a vast ecosystem of modules and tools, so this cheatsheet is by no means exhaustive. Nonetheless, it should provide a useful reference for developers starting with Node.js.

Advertisment
StudioPress
Ad - SiteGround Web Hosting - Crafted for easy site management. Amazing Speed; Powerful Tools; Top-rated support. Learn more.
rocket.net