Interview Q&A / Node.js
Node.js interview questions
The event loop on the server, streams, modules and building APIs with Express.
Prefer rapid revision? Practise Node.js short interview answers →
- If Node.js is single-threaded, how does it handle thousands of requests at the same time?
Your JavaScript runs on one main thread, but slow work like network and file I/O is handed to the operating system or libuv's thread pool. The main thread never sits waiting; it keeps taking new requests and runs each callback when its I/O finishes.
BeginnerIn-depth3 min read - What is middleware in Express.js and how does next() work?
Middleware is a function that runs between the request arriving and the response being sent. It receives req, res and next, and it either ends the response or calls next() to pass control to the next middleware or route. Calling next(err) jumps to error-handling middleware.
BeginnerIn-depth2 min read - What is the difference between CommonJS (require) and ES modules (import) in Node.js?
CommonJS uses require and module.exports, loads files synchronously, and was Node's original system. ES modules use import and export, are the JavaScript standard, are analysed before running, and support top-level await. Node supports both.
BeginnerIn-depth3 min read - What is the difference between dependencies and devDependencies, and why do we need package-lock.json?
dependencies are packages your app needs to run in production; devDependencies are only needed while developing, testing or building. package-lock.json records the exact version of every installed package so every machine and deployment gets the same dependency tree.
BeginnerIn-depth3 min read - How do you check whether a file exists in Node.js?
Use fs.promises.access in a try/catch, or fs.existsSync for quick sync checks. Better still, skip the check and handle the ENOENT error when you open the file, because the file can change between check and use.
BeginnerQuick answer1 min read - How do you convert a string to base64 and back in Node.js?
Use Buffer.from(text, "utf8").toString("base64") to encode and Buffer.from(b64, "base64").toString("utf8") to decode. Base64 is an encoding for transport, not encryption.
BeginnerQuick answer1 min read - How do you create a basic HTTP server in Node.js without Express?
Use the built-in http module: http.createServer takes a handler with request and response objects, and listen starts it on a port. You set status and headers on the response and finish it with end.
BeginnerQuick answer1 min read - How do you get __dirname and __filename inside an ES module?
They do not exist in ES modules. Use import.meta.url with fileURLToPath and path.dirname, or on recent Node versions import.meta.dirname and import.meta.filename.
BeginnerQuick answer1 min read - How do you list all files in a folder and its subfolders in Node.js?
Use fs.promises.readdir with the recursive option on recent Node versions, or write a small recursive function using withFileTypes to tell files from directories.
BeginnerQuick answer1 min read - How do you return a custom 404 response for unknown routes in Express?
Add a middleware with no path after all your routes. If no earlier route replied, the request reaches it, and you send a 404 status with a JSON or HTML body.
BeginnerQuick answer1 min read - How do you serve static files such as images and CSS in Express?
Use the built-in express.static middleware with a folder path. Files inside that folder are served directly by URL, and you can add a prefix and caching options.
BeginnerQuick answer1 min read - How do you tell Node.js to treat your files as ES modules?
Set "type": "module" in package.json so .js files are ESM, or use the .mjs extension for individual files. Use .cjs to force CommonJS inside an ESM project.
BeginnerQuick answer1 min read - Is the payload of a JWT encrypted, and what should you not put in it?
No. The payload is only base64url encoded, so anyone holding the token can read it. Keep it small and non-sensitive: user id, role, expiry. Never put passwords, card details or personal secrets in it.
BeginnerQuick answer1 min read - What are good URL naming conventions for REST resources?
Use plural nouns for resources, let the HTTP method express the action, nest only one level for relationships, and use lowercase with hyphens. So GET /students/5/courses, not /getStudentCourses?id=5.
BeginnerQuick answer1 min read - What are npm scripts and how do you run them?
Scripts are named shell commands in the scripts field of package.json. Run them with npm run name; start and test also work as npm start and npm test. Local binaries in node_modules/.bin are on the PATH automatically.
BeginnerQuick answer1 min read - What are the four types of streams in Node.js?
Readable (data source, like a file read), Writable (destination, like a file write or HTTP response), Duplex (both, like a TCP socket), and Transform (a duplex that changes data as it passes, like gzip).
BeginnerQuick answer1 min read - What are the three parts of a JWT?
A JWT has a header (algorithm and type), a payload (claims such as user id and expiry), and a signature. They are base64url-encoded and joined with dots. The signature proves the token was not changed.
BeginnerQuick answer1 min read - What do the caret (^) and tilde (~) mean in package.json versions?
Caret allows updates that do not change the leftmost non-zero version number, so ^1.4.2 accepts 1.x.x at or above 1.4.2. Tilde allows only patch updates, so ~1.4.2 accepts 1.4.x at or above 1.4.2.
BeginnerQuick answer1 min read - What does npm audit do and how should you act on its results?
npm audit checks your dependency tree against a database of known vulnerabilities and reports severity and fixes. Use npm audit fix for safe upgrades, and review breaking or unreachable issues by hand.
BeginnerQuick answer1 min read - What does the main field in package.json do?
main names the entry file that Node loads when someone requires or imports your package by name. If it is missing, Node falls back to index.js. Modern packages may also use the exports field.
BeginnerQuick answer1 min read - What does util.promisify do and when is it useful?
It converts a function that takes an error-first callback as its last argument into one that returns a promise, so older callback APIs can be used with async/await.
BeginnerQuick answer1 min read - What happens when you require the same module twice in Node.js?
The file runs only once. Node caches the exported object in require.cache, so later require calls return the same object, which means shared state between files.
BeginnerQuick answer1 min read - What happens when you run a heavy CPU loop inside a Node.js request handler?
The single JavaScript thread is busy, so the event loop cannot serve any other request until the loop ends. Every user sees delays. Move heavy work to worker threads, a queue, or break it into chunks.
BeginnerQuick answer1 min read - What is a Buffer in Node.js and why does it exist?
A Buffer is a fixed-size chunk of raw memory holding bytes. Node uses it for binary data such as files, network packets and images, because JavaScript strings are meant for text, not raw bytes.
BeginnerQuick answer1 min read - What is callback hell and how do you avoid it?
Callback hell is deeply nested callbacks where each step lives inside the previous one, making code hard to read and handle errors in. Avoid it with named functions, promises, and async/await.
BeginnerQuick answer1 min read - What is express.Router and why use it?
express.Router creates a mini app that holds a group of related routes and middleware. You mount it under a path prefix, which keeps large apps organised by feature.
BeginnerQuick answer1 min read - What is npx and when would you use it instead of npm install?
npx runs a package's command, using a local copy if present or downloading it temporarily. It is good for one-off tools and project generators, so you avoid installing them globally.
BeginnerQuick answer1 min read - What is semantic versioning and why does it matter for npm packages?
Semantic versioning uses MAJOR.MINOR.PATCH. Increase PATCH for bug fixes, MINOR for backward-compatible features, MAJOR for breaking changes, so users can judge upgrade risk from the number alone.
BeginnerQuick answer1 min read - What is the difference between fs.readFileSync, fs.readFile and fs.promises.readFile?
readFileSync blocks the thread until done. readFile takes a callback and does not block. fs.promises.readFile returns a promise for use with await. In servers, prefer the non-blocking forms.
BeginnerQuick answer1 min read - What is the difference between module.exports and exports in CommonJS?
exports is just a shortcut variable pointing at module.exports. You can add properties to either, but only module.exports is what require returns, so reassigning exports alone exports nothing.
BeginnerQuick answer1 min read