Top Node.js Interview Questions
Top 50 Node.js interview questions with curated answers across core concepts, practical scenarios, and interview-ready fundamentals.
Top 50 Node.js Interview Questions
- For an application that receives a continuous stream of very large (multi-gigabyte) binary data, which is the most memory-efficient approach to concatenating the incoming chunks?
- A Node.js application needs to resolve a hostname to its IP address. Which of the following approaches is most robust in handling potential errors?
- From a security perspective, what is the primary purpose of an `.npmrc` file in a project that uses both public and private package registries?
- For which of the following scenarios would an in-memory database (like SQLite in-memory) be the MOST appropriate choice for integration testing?
- A Node.js service is experiencing OutOfMemory errors, but monitoring shows the total free memory is still high. What is the most likely cause?
- How are environment variables typically provided to a Node.js application running inside a Docker container?
- How can you determine the current working directory of a Node.js process and how might this differ between development and production environments?
- A package has the version number 2.3.1-beta.2. According to SemVer, which of the following statements is TRUE?
- For transforming a very large file from uppercase to lowercase, which approach is the MOST memory-efficient?
- How can Cross-Site Request Forgery (CSRF) attacks be mitigated when using JWTs stored in cookies?
- Identify the logical error in the following Node.js code that uses `child_process.fork` for a calculation. The parent process logs the result, but it will always log `undefined`.
- Identify the critical security vulnerability in this Node.js code snippet that uses an environment variable.
- A script with an active inspector session exits unexpectedly using `process.exit()`. The developer notices that cleanup logic after the exit call is not run. Why does this happen?
- Identify the primary performance issue in the following code snippet.
- Identify the primary issue in this Node.js code snippet that attempts to handle signals.
- Identify the critical security vulnerability in the following Node.js code snippet that constructs an SQL query:
- Describe a scenario where using `async_hooks` could lead to unexpected performance issues. Provide a concrete example and explain how to mitigate these problems.
- Describe a scenario where using `async_hooks` might be beneficial for debugging a complex Node.js application, and explain how you would implement it.
- Describe a scenario where using `async_hooks` to track asynchronous operations could create a performance bottleneck. How could you mitigate this issue?
- Describe a practical application of `AsyncLocalStorage` beyond basic logging. Consider scenarios involving custom metrics or distributed tracing in a complex system.
- A Node.js server is crashing due to memory exhaustion when handling large file uploads. Identify the likely cause in the code and explain how to fix it using streams.
- Aside from raw performance, what is a key architectural difference between the Bun runtime and the Node.js runtime?
- Compare and contrast Sinon.js and Jest for mocking in a Node.js E2E testing environment.
- Can an event listener be an `async` function? If so, explain how errors should be handled within it to prevent unhandled promise rejections.
- Compare and contrast Node.js's `EventEmitter` with the `EventTarget` API found in modern web browsers. What are the key similarities and differences?
- Compare and contrast schema stitching and Apollo Federation as strategies for composing GraphQL APIs from microservices.
- Can you use the `http2` module to create an unencrypted HTTP/2 server (`h2c`)? If so, how, and what are the practical limitations?
- Beyond simple mocking, what is 'Service Virtualization' and how can it be used to create more realistic integration tests for a Node.js application that depends on third-party APIs?
- Beyond just finding a module's absolute path, how can the `require.resolve.paths()` method be used for debugging module resolution issues in a complex project?
- Compare and contrast Synthetic Monitoring with Real User Monitoring (RUM). In what scenarios would you prioritize one over the other for a Node.js web application?
- Compare and contrast how CommonJS and ES Modules handle circular dependencies.
- Describe a scenario where insecure deserialization could lead to a Remote Code Execution (RCE) vulnerability in a Node.js application. Explain how to prevent this vulnerability.
- Can an ORM interact with database views? If so, explain how this is typically accomplished and what the limitations are.
- Compare and contrast monolithic and microservices architectures for building a backend system. What are the key implications for REST API design in a microservices architecture?
- Beyond the standard joins, explain the purpose and provide practical use cases for both a `CROSS JOIN` and a `SELF JOIN` in SQL. Illustrate with examples.
- Beyond basic cookie settings, describe three advanced security measures or considerations you should implement to harden a session management system against sophisticated attacks.
- Describe a scenario where using a `PassThrough` stream would be beneficial. Explain your reasoning and provide a code example.
- Besides `pipeline`, what is the `stream.finished` utility used for, and why is it important for preventing resource leaks?
- Can the `StringDecoder`'s internal buffer grow indefinitely and cause a memory leak? Explain why or why not.
- Beyond basic logging, what specific tools can be used to effectively debug asynchronous errors in a production Node.js application?
- Compare and contrast Mocks and Stubs as types of test doubles. Provide a specific Node.js testing scenario where a Mock would be more appropriate than a Stub, and vice versa.
- Compare and contrast Spies and Fakes as types of test doubles. Provide a specific Node.js testing scenario where a Spy would be more appropriate than a Fake, and vice versa.
- Can code inside a `vm` sandbox access the filesystem? Explain why or why not, and how you might safely provide such functionality.
- A valid signature is failing verification. What are common non-cryptographic reasons for this, and how can they be mitigated?
- Compare and contrast `crypto.subtle.wrapKey` and `crypto.subtle.encrypt`. When would you choose one over the other?
- Describe a scenario where a seemingly innocuous dependency could introduce a significant security vulnerability into your Node.js application, even if it passes all automated security scans.
- Describe a scenario where using `assert.doesNotThrow` could lead to unexpected test failures or false positives. Provide an example and explain how to mitigate the issue.
- Explain why throwing a custom `RangeError` is a better approach than using `assert` for runtime input validation in the given function.
- Analyze the following `package.json`. Identify the security vulnerabilities related to its dependency management and suggest improvements.
- Analyze the following `package.json` file. Identify the security vulnerabilities related to dependency management and suggest improvements. Consider both direct and transitive dependencies.