在注入的主脚本中加载模块
🌐 Module loading in the injected main script
在注入的主脚本中,模块加载不会从文件系统读取。默认情况下,require() 和 import 语句只能加载内置模块。尝试加载只能在文件系统中找到的模块会抛出错误。
🌐 In the injected main script, module loading does not read from the file system.
By default, both require() and import statements would only be able to load
the built-in modules. Attempting to load a module that can only be found in the
file system will throw an error.
用户可以将他们的应用打包成一个独立的 JavaScript 文件,以注入到可执行文件中。这也确保了更确定的依赖图。
🌐 Users can bundle their application into a standalone JavaScript file to inject into the executable. This also ensures a more deterministic dependency graph.
为了在注入的主脚本中从文件系统加载模块,用户可以创建一个 require 函数,该函数可以使用 module.createRequire() 从文件系统加载模块。例如,在 CommonJS 入口点中:
🌐 To load modules from the file system in the injected main script, users can
create a require function that can load from the file system using
module.createRequire(). For example, in a CommonJS entry point:
const { createRequire } = require('node:module');
require = createRequire(__filename);