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 →
- 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 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 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 - How can CommonJS code load an ES module?
Use dynamic import(), which works in CommonJS and returns a promise. Plain require of an ES module was not allowed historically; newer Node versions added limited support, so dynamic import is the safe choice.
IntermediateQuick answer1 min read - What is a circular dependency in Node.js and what happens when it occurs?
A circular dependency is when module A requires B and B requires A. In CommonJS, the second module receives A's partially filled exports at that moment, so values defined later are undefined. Fix it by restructuring.
IntermediateQuick answer1 min read