All topics
Backend · Learning hub

Node.js notes for developers

Master Node.js with a curated set of 3 developer notes — core concepts, patterns, and interview prep. Maintained by the DevRecall team.

Save this stack to your DevRecallMore Backend notes
Node.js

Interview Questions

Node.js Interview Questions Fundamentals 1. What is Node.js? Node.js is a JavaScript runtime built on Chrome's V8 engine. It enables JavaScript to run on the se

Node.js Interview Questions

Fundamentals

1. What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 engine. It enables JavaScript to run on the server, with non-blocking I/O and event-driven architecture.

2. How does the event loop work?

The event loop processes callbacks from the event queue. Phases: timers, pending callbacks, poll, check, close callbacks. It allows async operations without threads.

3. What is the difference between process.nextTick and setImmediate?

process.nextTick executes callback after current operation, before event loop continues. setImmediate executes in check phase of event loop.

4. What are streams?

Streams handle reading/writing data piece by piece. Types: Readable, Writable, Duplex, Transform. Efficient for large files.

5. What is clustering?

Clustering spawns child processes to handle load across CPU cores. Master process distributes connections to worker processes.

Node.js

Event Loop & Async Programming

Node.js Event Loop & Async Programming The event loop is the heart of Node.js. It enables non-blocking I/O operations despite JavaScript being single-threaded.

Node.js Event Loop & Async Programming

The event loop is the heart of Node.js. It enables non-blocking I/O operations despite JavaScript being single-threaded.

The Event Loop

// Event loop phases:
// 1. Timers (setTimeout, setInterval)
// 2. Pending callbacks (I/O callbacks)
// 3. Idle, prepare
// 4. Poll (retrieve new I/O events)
// 5. Check (setImmediate)
// 6. Close callbacks

console.log('1 - Start');

setTimeout(() => {
  console.log('2 - setTimeout');
}, 0);

setImmediate(() => {
  console.log('3 - setImmediate');
});

process.nextTick(() => {
  console.log('4 - nextTick');
});

Promise.resolve().then(() => {
  console.log('5 - Promise');
});

console.log('6 - End');

// Output:
// 1 - Start
// 6 - End
// 4 - nextTick
// 5 - Promise
// 2 - setTimeout
// 3 - setImmediate

Callbacks, Promises, Async/Await

// Callback pattern (old)
const fs = require('fs');

fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(data);
});

// Promise pattern
const fsPromises = require('fs/promises');

fsPromises.readFile('file.txt', 'utf8')
  .then(data => console.log(data))
  .catch(err => console.error(err));

// Async/await (modern)
async function readFile() {
  try {
    const data = await fsPromises.readFile('file.txt', 'utf8');
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

// Promisify callback-based functions
const { promisify } = require('util');
const readFilePromise = promisify(fs.readFile);

async function read() {
  const data = await readFilePromise('file.txt', 'utf8');
  return data;
}

Error Handling

// Try-catch for sync errors
try {
  const data = JSON.parse(invalidJson);
} catch (error) {
  console.error('Parse error:', error);
}

// Try-catch for async/await
async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Fetch error:', error);
    throw error;
  }
}

// Unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
  // Application specific logging, throwing an error, or other logic here
});

// Uncaught exceptions
process.on('uncaughtException', (error) => {
  console.error('Uncaught Exception:', error);
  // Perform cleanup and exit
  process.exit(1);
});
Node.js

Modules & NPM Ecosystem

Node.js Modules & NPM CommonJS vs ES Modules // CommonJS (default in Node.js) // utils.js function add(a, b) { return a + b; } module.exports = { add }; // or:

Node.js Modules & NPM

CommonJS vs ES Modules

// CommonJS (default in Node.js)
// utils.js
function add(a, b) {
  return a + b;
}

module.exports = { add };
// or: exports.add = add;

// app.js
const { add } = require('./utils');
console.log(add(2, 3));

// ES Modules (requires "type": "module" in package.json)
// utils.mjs (or .js with "type": "module")
export function add(a, b) {
  return a + b;
}

export default function multiply(a, b) {
  return a * b;
}

// app.mjs
import multiply, { add } from './utils.mjs';
console.log(add(2, 3));
console.log(multiply(2, 3));

NPM Package Management

# Initialize project
npm init -y

# Install dependencies
npm install express
npm install -D typescript @types/node

# Install specific version
npm install lodash@4.17.21

# Update packages
npm update
npm update express

# Remove package
npm uninstall express

# List installed packages
npm list
npm list --depth=0

# Run scripts
npm run dev
npm start
npm test

Keep your Node.js knowledge sharp.

Save this stack to your personal DevRecall — add your own notes, track what you're learning, and share what you know with the community.

Get started — free forever