Skip to content

Commit b993d9e

Browse files
committed
doc: specify vfs precedence in module resolution
1 parent 3b4d428 commit b993d9e

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

doc/api/vfs.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,35 @@ The promise namespace mirrors `fs.promises` and includes `readFile`,
293293
## Module loader integration
294294

295295
Once a `VirtualFileSystem` is mounted, paths under the mount point
296-
participate in module resolution and loading. Both
297-
`require()` / `require.resolve()` (CommonJS) and `import` /
298-
`import.meta.resolve()` (ECMAScript modules) consult the VFS through
299-
the same toggleable hooks that `node:fs` uses, so files served from
300-
the VFS are first-class modules: `package.json` is honoured,
301-
extensionless files are sniffed for Wasm vs. JavaScript, conditional
302-
`exports` / `imports` work, and so on.
296+
participate in module resolution and loading. The [CommonJS
297+
resolution algorithm][] used by [`require()`][] and
298+
[`require.resolve()`][] and the [ES modules resolution algorithm][]
299+
used by `import` and [`import.meta.resolve()`][] are unchanged;
300+
instead, every file system operation those algorithms perform
301+
(existence probes, file reads, `package.json` lookups, real-path
302+
resolution) is dispatched on the path being probed: paths under a
303+
mount point are served by the owning VFS, and all other paths are
304+
served by the real file system. Files served from the VFS therefore
305+
behave as first-class modules: `package.json` is honoured,
306+
conditional [`"exports"`][] / [`"imports"`][] work, and so on.
307+
308+
Because mounted paths live in a reserved namespace that cannot exist
309+
on disk, any given path is served either by exactly one VFS or by
310+
the real file system, never both. There is no search order or
311+
fallback between the two: if a path under a mount point does not
312+
exist in the VFS, resolution fails with `ENOENT` without consulting
313+
the disk, and a mounted layer never shadows a real directory.
314+
315+
For resolution purposes the mount point behaves as a file system
316+
root: `package.json` scope lookups (for [`"type"`][],
317+
[`"exports"`][], [`"imports"`][], and the CommonJS directory
318+
[`"main"`][]) and [loading from `node_modules` folders][] stop at
319+
the mount point instead of continuing into the real file system
320+
([the global folders][], such as `NODE_PATH`, are legacy CommonJS
321+
behavior and still apply to `require()`). Absolute specifiers may
322+
cross the boundary in either direction: a module on the real file
323+
system can `require()` a mounted path, and a virtual module can
324+
`require()` a real one.
303325

304326
```cjs
305327
const vfs = require('node:vfs');
@@ -361,9 +383,6 @@ the caller's responsibility to avoid.
361383
added: v26.4.0
362384
-->
363385

364-
The base class for all VFS providers. Subclasses implement the essential
365-
primitives (`open`, `stat`, `readdir`, `mkdir`, `rmdir`, `unlink`,
366-
`rename`, ...) and inherit default implementations of the derived
367386
The base class for all VFS providers. Subclasses implement the essential
368387
primitives (such as `open`, `stat`, `readdir`, `mkdir`, `rmdir`, `unlink`,
369388
`rename`, etc.) and inherit default implementations of the derived
@@ -478,13 +497,24 @@ fields use synthetic but stable values:
478497
* `blocks` is `Math.ceil(size / 512)`.
479498
* Times default to the moment the entry was created/last modified.
480499

500+
[CommonJS resolution algorithm]: modules.md#all-together
501+
[ES modules resolution algorithm]: esm.md#resolution-algorithm
481502
[Explicit Resource Management]: https://github.com/tc39/proposal-explicit-resource-management
503+
[`"exports"`]: packages.md#exports
504+
[`"imports"`]: packages.md#imports
505+
[`"main"`]: packages.md#main
506+
[`"type"`]: packages.md#type
482507
[`MemoryProvider`]: #class-memoryprovider
483508
[`RealFSProvider`]: #class-realfsprovider
484509
[`VirtualFileSystem`]: #class-virtualfilesystem
485510
[`VirtualProvider`]: #class-virtualprovider
486511
[`fs.BigIntStats`]: fs.md#class-fsbigintstats
487512
[`fs.Stats`]: fs.md#class-fsstats
513+
[`import.meta.resolve()`]: esm.md#importmetaresolvespecifier
488514
[`node:fs`]: fs.md
515+
[`require()`]: modules.md#requireid
516+
[`require.resolve()`]: modules.md#requireresolverequest-options
489517
[`vfs.mount()`]: #vfsmount
490518
[`vfs.unmount()`]: #vfsunmount
519+
[loading from `node_modules` folders]: modules.md#loading-from-node_modules-folders
520+
[the global folders]: modules.md#loading-from-the-global-folders

0 commit comments

Comments
 (0)