Resolving core vs relative module specifiers
knowledge of module resolution rules.
'fs' is a built-in core module loaded by name with top priority; './my-file.js' is a relative path resolved from the current file.
What's really being asked
This verifies that you know how Node decides what a require string points to. The resolution algorithm has clear precedence rules, and confusing core modules with packages or relative files is a telling gap.
The full answer
A specifier without a path prefix, like fs, is a bare specifier. Node checks its set of built-in core modules first; fs, path, http, and similar are compiled into the Node binary, so requiring them returns the built-in immediately with no filesystem access and the highest priority. A specifier starting with ./ or ../ is relative and is resolved against the directory of the file doing the requiring. Node tries the literal path, then appends extensions such as .js, .json, and .node, and if the target is a directory it looks for a main field or index file. A bare name that is not core triggers the node_modules lookup, walking up parent directories until the package is found.
The mistakes people make
Saying fs is installed in node_modules, that core modules can be shadowed by a same-named local package, or that relative and bare specifiers follow the same search path. Another error is forgetting the extension and index resolution steps for relative paths.
What usually comes next
The full node_modules walk, the node: prefix for core modules, how package.json exports maps change resolution, and how ES module resolution differs from CommonJS.
A concrete example
From a file at src/api/routes.js, require('fs') returns the built-in immediately, while require('./users.js') resolves to src/api/users.js. If you instead wrote require('users'), Node would ignore that local file and search src/api/node_modules, then src/node_modules, then node_modules, looking for an installed package named users.
Interview question
What does Node do first when it encounters require('fs')?
- a.Matches it against built-in core modules, which take precedenceCorrect
- b.Downloads it from the npm registry on demand
- c.Treats it as a relative path from the current file
- d.Searches node_modules directories upward for an fs package
Why? this is the answer
Bare specifiers are checked against built-in core modules first; fs is compiled into the binary and resolves without touching node_modules. The package search only runs for non-core bare names.
Just read this? Test yourself on what you have been reading.
Read the original → nodejs.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.
See open roles