Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. What is the difference between npm install and npm ci?

    npm install can update the lock file and resolves versions from package.json. npm ci deletes node_modules and installs exactly what package-lock.json says, failing if the two disagree, which makes builds repeatable.

    BeginnerQuick answer1 min read
  28. What is the difference between path.join and path.resolve?

    join just glues segments together with the correct separator. resolve builds an absolute path, processing segments right to left until it has one, and falls back to the current working directory.

    BeginnerQuick answer1 min read
  29. What is the difference between Promise.all and Promise.allSettled?

    Promise.all rejects as soon as any promise rejects and gives you results only if all succeed. Promise.allSettled always waits for every promise and reports each outcome as fulfilled or rejected.

    BeginnerQuick answer1 min read
  30. What is the difference between PUT and PATCH in a REST API?

    PUT replaces the whole resource with the body you send, so missing fields are cleared. PATCH changes only the fields you send. PUT is idempotent by definition; PATCH is not guaranteed to be.

    BeginnerQuick answer1 min read