--vfs-load=source
source<string> 要挂载和运行的目录或归档文件。
需要--experimental-vfs。最多只能使用一次。
🌐 Requires --experimental-vfs. May be given at most once.
将 source 挂载为一个虚拟文件系统(node:vfs),并在该挂载下运行入口点以及所有后续的 require()/import 解析,而不是在真实文件系统上运行。挂载点位于 Node.js 分配的保留挂载点,因此它从不覆盖真实路径,也无法选择目标。入口点从挂载中获取,就像 node <directory> 获取入口点一样:挂载自身的 package.json "main",或者 index.js。任何位置命令行参数都是程序自身的(从 process.argv[2] 开始可用),从不覆盖入口点。
🌐 Mounts source as a virtual file system (node:vfs), and runs the entry
point and all subsequent require()/import resolution against that mount
rather than the real file system. The mount is placed at a reserved mount point
assigned by Node.js, so it never shadows real paths and no target can be
chosen. The entry point is taken from the mount the same way node <directory>
takes one: the mount's own package.json "main", or index.js. Any
positional command-line argument is the program's own (available from
process.argv[2] onward), never an entry-point override.
process.argv[1] 报告的是 source 而不是保留的挂载点,因为挂载点是一个不透明的实现细节。
支持某个来源的提供者是从来源本身选择的,而不是从它的文件名选择的:
🌐 The provider backing a source is chosen from the source itself rather than from its file name:
- 一个目录被挂载了,根目录是
RealFSProvider。 - 一个字节是 ZIP 压缩文件的文件通过
ZipProvider挂载,所以压缩包可以有任何名字。
首先会咨询注册了 vfs.registerProvider() 的提供者(通常来自预加载了 --require 或 --import 的模块),按照注册的逆序进行,并且他们可以声明目录和文件。如果没有提供者声明这个源,Node.js 就会报错退出。
🌐 Providers registered with vfs.registerProvider() (typically from a module
preloaded with --require or --import) are consulted first, in
reverse registration order, and may claim directories as well as files. If no
provider claims the source, Node.js exits with an error.
在工作线程中,--vfs-load 会挂载但不会加载:工作线程会继承挂载并运行它自己的入口点,这个入口点本身可能也存在于挂载中。
🌐 In worker threads --vfs-load mounts but does not load: a worker inherits the
mount and runs its own entry point, which may itself live in the mount.
无论线程挂载了什么,源都会挂在每个挂载它的线程使用的相同保留挂载点上,所以进入挂载的路径在它们之间都是一样的。
🌐 The source is mounted at the same reserved mount point in every thread that mounts it, whatever else that thread mounts, so a path into the mount means the same thing in all of them.
使用自己的 execArgv 创建的工作线程不会继承父线程的任何选项,因此根本不会挂载源。要从挂载中运行脚本,必须再次给这个工作线程相同的选项 --experimental-vfs 和 --vfs-load;否则,这个线程没有脚本来源,工作线程就无法加载脚本。--experimental-vfs 还让工作线程自己的代码可以使用 node:vfs。脚本来自其他地方的工作线程,比如实际文件系统,就不需要额外的设置。
🌐 A worker created with its own execArgv inherits none of the parent's options,
and so does not mount the source at all. To run a script from the mount, such a
worker must be given the same options again, --experimental-vfs and
--vfs-load; without them, that thread has no mount for the script to come
from, and the worker fails to load it. --experimental-vfs is also what makes
node:vfs available to the worker's own code. A worker whose script comes
from anywhere else, such as the real file system, needs nothing added.
--vfs-load 在 NODE_OPTIONS 中是不允许的:哪个入口点运行是由命令行决定的,环境不能重定向它。
$ node --experimental-vfs --vfs-load=app.zip