File tree 2 files changed +28
-5
lines changed
2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -12,10 +12,12 @@ const {
12
12
ReflectApply,
13
13
SafeArrayIterator,
14
14
SafeSet,
15
+ StringPrototypeEndsWith,
15
16
StringPrototypeIncludes,
16
17
StringPrototypeMatch,
17
18
StringPrototypeReplace,
18
19
StringPrototypeSplit,
20
+ StringPrototypeStartsWith,
19
21
} = primordials ;
20
22
21
23
const { ModuleWrap } = internalBinding ( 'module_wrap' ) ;
@@ -154,11 +156,18 @@ class ModuleJob {
154
156
try {
155
157
await this . module . evaluate ( timeout , breakOnSigint ) ;
156
158
} catch ( e ) {
157
- if ( e . name === 'ReferenceError' &&
158
- e . message === 'require is not defined' ) {
159
- e . message += ' in ES module scope. You can use `await import` instead' +
160
- ', or use `.cjs` extension to load the file as CommonJS ' +
161
- 'module.' ;
159
+ if ( e . name === 'ReferenceError' && (
160
+ e . message === 'require is not defined' ||
161
+ e . message === 'module is not defined'
162
+ ) ) {
163
+ e . message += ' in ES module scope. You can use `await import` instead' ;
164
+
165
+ if ( StringPrototypeEndsWith ( this . module . url , '.js' ) &&
166
+ StringPrototypeStartsWith ( this . module . url , 'file://' ) ) {
167
+ e . message +=
168
+ '. It seems you are trying to load a file using `.js` extension ' +
169
+ 'inside a folder containing a `package.json`; you need to use ' + 'the `.cjs` extension to load the file as a CommonJS module' ;
170
+ }
162
171
}
163
172
throw e ;
164
173
}
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
const common = require ( '../common' ) ;
3
+ const fixtures = require ( '../common/fixtures' ) ;
3
4
const assert = require ( 'assert' ) ;
4
5
5
6
assert . rejects (
6
7
import ( 'data:text/javascript,require;' ) ,
7
8
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
8
9
/ u s e .a w a i t i m p o r t . i n s t e a d /
9
10
) . then ( common . mustCall ( ) ) ;
11
+
12
+ assert . rejects (
13
+ import ( 'data:text/javascript,require;//.js' ) ,
14
+ // eslint-disable-next-line node-core/no-unescaped-regexp-dot
15
+ / ^ (? ! I t s e e m s y o u a r e t r y i n g t o l o a d a f i l e u s i n g ` .j s ` e x t e n s i o n ) .* $ /
16
+ ) . then ( common . mustCall ( ) ) ;
17
+
18
+ assert . rejects (
19
+ import ( fixtures . path ( '/es-modules/package-type-module/cjs.js' ) ) ,
20
+ // eslint-disable-next-line node-core/no-unescaped-regexp-dot
21
+ / u s e t h e .\. c j s . e x t e n s i o n t o l o a d t h e f i l e a s a C o m m o n J S m o d u l e /
22
+ ) . then ( common . mustCall ( ) ) ;
23
+
You can’t perform that action at this time.
0 commit comments