Description
What steps will reproduce the bug?
Imagine that the fs
module is overriden to add support for a virtual filesystem. Make a require
call towards a virtual file.
What is the expected behavior?
The resolution should succeed, since the fs
methods know how to access the file.
What do you see instead?
Cannot find module 'virtual'.
Additional information
This occurs because Node cheats and doesn't actually use all of the fs
methods. In most cases it does (for example fs.realpath
, fs.readFile
, fs.readdir
), but not for all. Two methods in particular goes into unique native bindings that cannot be overriden:
internalModuleStat
is used instead offs.stat
: https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L151internalModuleReadJSON
is used instead offs.readFile
:
https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L257
Since those functions aren't exposed (not only do they come from internalBinding
, they're also destructured
so we never have a chance to change their references), we cannot even add the virtual layer to them at all.
Would it be possible to use the fs
primitives or, at least, to expose internalModuleStat
& friends in a way we can wrap them?