@@ -57,6 +57,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
57
57
( globalLike ) => errorMessage === `${ globalLike } is not defined` ,
58
58
) ;
59
59
60
+
61
+ /**
62
+ *
63
+ * @param {Error } e
64
+ * @param {string } url
65
+ * @returns {void }
66
+ */
67
+ const explainCommonJSGlobalLikeNotDefinedError = ( e , url ) => {
68
+ if ( e ?. name === 'ReferenceError' &&
69
+ isCommonJSGlobalLikeNotDefinedError ( e . message ) ) {
70
+ e . message += ' in ES module scope' ;
71
+
72
+ if ( StringPrototypeStartsWith ( e . message , 'require ' ) ) {
73
+ e . message += ', you can use import instead' ;
74
+ }
75
+
76
+ const packageConfig =
77
+ StringPrototypeStartsWith ( url , 'file://' ) &&
78
+ RegExpPrototypeExec ( / \. j s ( \? [ ^ # ] * ) ? ( # .* ) ? $ / , url ) !== null &&
79
+ require ( 'internal/modules/package_json_reader' )
80
+ . getPackageScopeConfig ( url ) ;
81
+ if ( packageConfig . type === 'module' ) {
82
+ e . message +=
83
+ '\nThis file is being treated as an ES module because it has a ' +
84
+ `'.js' file extension and '${ packageConfig . pjsonPath } ' contains ` +
85
+ '"type": "module". To treat it as a CommonJS script, rename it ' +
86
+ 'to use the \'.cjs\' file extension.' ;
87
+ }
88
+ }
89
+ } ;
90
+
60
91
class ModuleJobBase {
61
92
constructor ( url , importAttributes , isMain , inspectBrk ) {
62
93
this . importAttributes = importAttributes ;
@@ -271,27 +302,7 @@ class ModuleJob extends ModuleJobBase {
271
302
try {
272
303
await this . module . evaluate ( timeout , breakOnSigint ) ;
273
304
} catch ( e ) {
274
- if ( e ?. name === 'ReferenceError' &&
275
- isCommonJSGlobalLikeNotDefinedError ( e . message ) ) {
276
- e . message += ' in ES module scope' ;
277
-
278
- if ( StringPrototypeStartsWith ( e . message , 'require ' ) ) {
279
- e . message += ', you can use import instead' ;
280
- }
281
-
282
- const packageConfig =
283
- StringPrototypeStartsWith ( this . module . url , 'file://' ) &&
284
- RegExpPrototypeExec ( / \. j s ( \? [ ^ # ] * ) ? ( # .* ) ? $ / , this . module . url ) !== null &&
285
- require ( 'internal/modules/package_json_reader' )
286
- . getPackageScopeConfig ( this . module . url ) ;
287
- if ( packageConfig . type === 'module' ) {
288
- e . message +=
289
- '\nThis file is being treated as an ES module because it has a ' +
290
- `'.js' file extension and '${ packageConfig . pjsonPath } ' contains ` +
291
- '"type": "module". To treat it as a CommonJS script, rename it ' +
292
- 'to use the \'.cjs\' file extension.' ;
293
- }
294
- }
305
+ explainCommonJSGlobalLikeNotDefinedError ( e , this . module . url ) ;
295
306
throw e ;
296
307
}
297
308
return { __proto__ : null , module : this . module } ;
@@ -397,27 +408,7 @@ class ModuleJobSync extends ModuleJobBase {
397
408
const namespace = this . module . evaluateSync ( ) ;
398
409
return { __proto__ : null , module : this . module , namespace } ;
399
410
} catch ( e ) {
400
- if ( e ?. name === 'ReferenceError' &&
401
- isCommonJSGlobalLikeNotDefinedError ( e . message ) ) {
402
- e . message += ' in ES module scope' ;
403
-
404
- if ( StringPrototypeStartsWith ( e . message , 'require ' ) ) {
405
- e . message += ', you can use import instead' ;
406
- }
407
-
408
- const packageConfig =
409
- StringPrototypeStartsWith ( this . module . url , 'file://' ) &&
410
- RegExpPrototypeExec ( / \. j s ( \? [ ^ # ] * ) ? ( # .* ) ? $ / , this . module . url ) !== null &&
411
- require ( 'internal/modules/package_json_reader' )
412
- . getPackageScopeConfig ( this . module . url ) ;
413
- if ( packageConfig . type === 'module' ) {
414
- e . message +=
415
- '\nThis file is being treated as an ES module because it has a ' +
416
- `'.js' file extension and '${ packageConfig . pjsonPath } ' contains ` +
417
- '"type": "module". To treat it as a CommonJS script, rename it ' +
418
- 'to use the \'.cjs\' file extension.' ;
419
- }
420
- }
411
+ explainCommonJSGlobalLikeNotDefinedError ( e , this . module . url ) ;
421
412
throw e ;
422
413
}
423
414
}
0 commit comments