1212const {
1313 prepareMainThreadExecution,
1414} = require ( 'internal/process/pre_execution' ) ;
15- const { isExperimentalSeaWarningNeeded, isSea, isVfsEnabled } = internalBinding ( 'sea' ) ;
15+ const {
16+ isSea : isLoadingSea ,
17+ isVfsEnabled : isVfsEnabledFlag ,
18+ isExperimentalSeaWarningNeeded,
19+ } = internalBinding ( 'sea' ) ;
1620const { emitExperimentalWarning } = require ( 'internal/util' ) ;
1721const { emitWarningSync } = require ( 'internal/process/warning' ) ;
18- const { Module, wrapModuleLoad } = require ( 'internal/modules/cjs/loader' ) ;
22+ const { Module } = require ( 'internal/modules/cjs/loader' ) ;
1923const { compileFunctionForCJSLoader } = internalBinding ( 'contextify' ) ;
2024const { maybeCacheSourceMap } = require ( 'internal/source_map/source_map_cache' ) ;
2125const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
@@ -33,9 +37,8 @@ const path = require('path');
3337// command line (e.g. it could be provided via an API or bundled into the executable).
3438prepareMainThreadExecution ( false , true ) ;
3539
36- const isLoadingSea = isSea ( ) ;
37- const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded ( ) ;
38- if ( isExperimentalSeaWarningNeeded ( ) ) {
40+ const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded ;
41+ if ( isExperimentalSeaWarningNeeded ) {
3942 emitExperimentalWarning ( 'Single executable application' ) ;
4043}
4144
@@ -86,14 +89,21 @@ function embedderRunCjs(content, filename) {
8689
8790 // Patch the module to make it look almost like a regular CJS module
8891 // instance. When VFS is active, set the filename to a VFS path so that
89- // relative require('./foo.js') resolves under the VFS mount point.
92+ // relative require('./foo.js') resolves under the VFS mount point,
93+ // and use createRequire for a fully-featured require function.
94+ let requireFn ;
9095 if ( seaVfsActive && seaVfsMountPoint ) {
9196 customModule . filename = seaVfsMountPoint + '/' + path . basename ( filename ) ;
9297 customModule . paths = Module . _nodeModulePaths (
9398 path . dirname ( customModule . filename ) ) ;
99+ // Use createRequire so that require has all standard properties
100+ // (resolve, cache, etc.) and builtin loading flows through hooks.
101+ requireFn = Module . createRequire ( customModule . filename ) ;
102+ requireFn . main = customModule ;
94103 } else {
95104 customModule . filename = process . execPath ;
96105 customModule . paths = Module . _nodeModulePaths ( process . execPath ) ;
106+ requireFn = embedderRequire ;
97107 }
98108 embedderRequire . main = customModule ;
99109
@@ -104,7 +114,7 @@ function embedderRunCjs(content, filename) {
104114 // out parameter.
105115 return compiledWrapper (
106116 customModule . exports , // exports
107- embedderRequire , // require
117+ requireFn , // require
108118 customModule , // module
109119 customModule . filename , // __filename
110120 path . dirname ( customModule . filename ) , // __dirname
@@ -134,7 +144,7 @@ function initSeaVfs() {
134144 if ( seaVfsInitialized ) return ;
135145 seaVfsInitialized = true ;
136146
137- if ( ! isLoadingSea || ! isVfsEnabled ( ) ) return ;
147+ if ( ! isLoadingSea || ! isVfsEnabledFlag ) return ;
138148
139149 // Check if SEA has VFS support
140150 const { getSeaVfs } = require ( 'internal/vfs/sea' ) ;
@@ -148,18 +158,11 @@ function initSeaVfs() {
148158function embedderRequire ( id ) {
149159 const normalizedId = normalizeRequirableId ( id ) ;
150160 if ( normalizedId ) {
151- // Built-in module
152161 return require ( normalizedId ) ;
153162 }
154163
155- // When VFS is active, use wrapModuleLoad which flows through the
156- // registered Module.registerHooks({ resolve, load }) hooks in
157- // module_hooks.js, handling both absolute VFS paths and relative requires.
158- if ( isLoadingSea && seaVfsActive ) {
159- return wrapModuleLoad ( id , embedderRequire . main , false ) ;
160- }
161-
162- // No VFS - show warning and throw
164+ // When VFS is not active, only built-in modules are supported.
165+ // VFS-enabled SEAs use createRequire instead of embedderRequire.
163166 warnNonBuiltinInSEA ( ) ;
164167 throw new ERR_UNKNOWN_BUILTIN_MODULE ( id ) ;
165168}
0 commit comments