File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -12,10 +12,12 @@ const {
1212 ReflectApply,
1313 SafeArrayIterator,
1414 SafeSet,
15+ StringPrototypeEndsWith,
1516 StringPrototypeIncludes,
1617 StringPrototypeMatch,
1718 StringPrototypeReplace,
1819 StringPrototypeSplit,
20+ StringPrototypeStartsWith,
1921} = primordials ;
2022
2123const { ModuleWrap } = internalBinding ( 'module_wrap' ) ;
@@ -154,11 +156,18 @@ class ModuleJob {
154156 try {
155157 await this . module . evaluate ( timeout , breakOnSigint ) ;
156158 } 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+ }
162171 }
163172 throw e ;
164173 }
Original file line number Diff line number Diff line change 11'use strict' ;
22const common = require ( '../common' ) ;
3+ const fixtures = require ( '../common/fixtures' ) ;
34const assert = require ( 'assert' ) ;
45
56assert . rejects (
67 import ( 'data:text/javascript,require;' ) ,
78 // eslint-disable-next-line node-core/no-unescaped-regexp-dot
89 / u s e .a w a i t i m p o r t . i n s t e a d /
910) . 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