EasyConcept

What is Node.js and why is it called single-threaded?

Technology:
Node.js
Experience:
Intern,
Junior

Quick answer

Node.js is a JavaScript runtime built on Chrome's V8 engine that runs your JavaScript on a single main thread and uses non-blocking I/O, so one thread can handle many concurrent connections.

Detailed explanation

Your JavaScript code runs on one thread, so there is no shared-memory race condition between your own functions. But Node.js is not single-threaded overall: the libuv library handles network I/O through the operating system's asynchronous mechanisms and uses a thread pool (four threads by default) for file system access, DNS lookups, compression and some crypto functions.

When a request arrives and needs a database result, Node starts the operation, registers a callback and moves on to other work instead of waiting. This model is excellent for I/O-heavy applications such as APIs, chat and streaming, and poor for long CPU-bound computation on the main thread, which blocks every other request.

How to explain it in an interview

Node.js is a JavaScript runtime built on V8 with an event loop and libuv for I/O. Your JavaScript runs on one thread, but file, network and DNS work is handed to the operating system or a small thread pool, and the callbacks come back through the event loop. That makes Node very efficient for I/O-heavy servers, while CPU-heavy work needs worker threads or a separate service.

Key points

  • Your JavaScript runs on one thread
  • libuv uses the OS and a thread pool for I/O
  • Great for I/O-bound work, risky for CPU-bound work

Common mistakes

  • Saying Node.js is completely single-threaded. Only the JavaScript execution is; libuv uses a thread pool for some I/O and crypto work.
  • Running CPU-heavy loops, synchronous file calls or large JSON parsing on the request path, which blocks every other request.
  • Node.jsEasy

    What is middleware in Express and how does it work?

    Middleware are functions that receive the request, response and a next callback; they run in the order they are registered and can modify the request, end the response or pass control on with next().

    Junior 路 Mid-level 路 REST API
  • Node.jsEasy

    What is the difference between package.json and package-lock.json?

    package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.

    Intern 路 Junior 路 Programming Fundamentals
  • Node.jsEasy

    What is the difference between require and import in Node.js?

    require is the CommonJS module system, which loads modules synchronously at runtime, while import is the ES module standard, which is statically analysed, asynchronous and supports top-level await and tree shaking.

    Junior 路 Mid-level 路 Programming Fundamentals
  • Node.jsMedium

    What are streams in Node.js and why should you use them?

    Streams process data piece by piece as it arrives instead of loading it all into memory, which lets you handle large files and network data efficiently; they come in Readable, Writable, Duplex and Transform types.

    Mid-level 路 Senior 路 Performance
  • Node.jsMedium

    What is the difference between JWT and session-based authentication?

    Session authentication stores the login state on the server and gives the browser a session ID cookie, while JWT authentication gives the client a signed token that contains the user's claims so the server can verify it without storing state.

    Mid-level 路 Senior 路 Authentication