@@ -101,12 +101,16 @@ if (ENVIRONMENT_IS_PTHREAD) {
101
101
102
102
#if ENVIRONMENT_MAY_BE_NODE
103
103
if ( ENVIRONMENT_IS_NODE ) {
104
- #if PTHREADS || WASM_WORKERS
105
104
#if EXPORT_ES6
106
- var worker_threads = await import ( 'worker_threads' ) ;
107
- #else
108
- var worker_threads = require ( 'worker_threads' ) ;
105
+ // When building an ES module `require` is not normally available.
106
+ // We need to use `createRequire()` to construct the require()` function.
107
+ const { createRequire } = await import ( 'module' ) ;
108
+ /** @suppress {duplicate} */
109
+ var require = createRequire ( '/' ) ;
109
110
#endif
111
+
112
+ #if PTHREADS || WASM_WORKERS
113
+ var worker_threads = require ( 'worker_threads' ) ;
110
114
global . Worker = worker_threads . Worker ;
111
115
ENVIRONMENT_IS_WORKER = ! worker_threads . isMainThread ;
112
116
#if PTHREADS
@@ -191,21 +195,19 @@ if (ENVIRONMENT_IS_NODE) {
191
195
}
192
196
#endif
193
197
198
+ // These modules will usually be used on Node.js. Load them eagerly to avoid
199
+ // the complexity of lazy-loading.
200
+ var fs = require ( 'fs' ) ;
201
+ var nodePath = require ( 'path' ) ;
202
+
194
203
#if EXPORT_ES6
195
- var fs = await import ( 'fs' ) ;
196
- var nodePath = await import ( 'path' ) ;
197
- var url = await import ( 'url' ) ;
198
204
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
199
205
// since there's no way getting the current absolute path of the module when
200
206
// support for that is not available.
201
207
if ( ! import . meta. url . startsWith ( 'data:' ) ) {
202
- scriptDirectory = nodePath . dirname ( url . fileURLToPath ( import . meta. url ) ) + '/' ;
208
+ scriptDirectory = nodePath . dirname ( require ( ' url' ) . fileURLToPath ( import . meta. url ) ) + '/' ;
203
209
}
204
210
#else
205
- // These modules will usually be used on Node.js. Load them eagerly to avoid
206
- // the complexity of lazy-loading.
207
- var fs = require ( 'fs' ) ;
208
- var nodePath = require ( 'path' ) ;
209
211
scriptDirectory = __dirname + '/' ;
210
212
#endif
211
213
0 commit comments