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
| Command | Description |
|---|
| node app.js | Run a Node.js application |
| npm install | Install project dependencies defined in package.json |
| npm start | Start a Node.js application |
| npm run <script> | Run a script defined in the package.json file |
| node -v | Check the version of Node.js installed |
| npm -v | Check the version of npm installed |
Module Management
| Command | Description |
|---|
| 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 ls | List installed packages |
| npm outdated | Check for outdated packages |
| npm update | Update installed packages |
| npm audit | Check for known security vulnerabilities |
File System Operations
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| process.argv | Get the command-line arguments |
| process.env | Get 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
| Command | Description |
|---|
| 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
| Command | Description |
|---|
| 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 |
| debugger | Pause the execution and enter the debugger |
| node --inspect app.js | Start the Node.js debugger |
| node --prof app.js | Generate a CPU profiling report |
Testing
| Command | Description |
|---|
| 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 mocha | Install the Mocha testing framework |
| npm install --save-dev chai | Install 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.