@@ -12,6 +12,7 @@ const {
12
12
RegExp,
13
13
SafeMap,
14
14
SafeSet,
15
+ String,
15
16
StringPrototypeEndsWith,
16
17
StringPrototypeIncludes,
17
18
StringPrototypeIndexOf,
@@ -48,6 +49,7 @@ const {
48
49
ERR_INVALID_PACKAGE_TARGET ,
49
50
ERR_MODULE_NOT_FOUND ,
50
51
ERR_PACKAGE_PATH_NOT_EXPORTED ,
52
+ ERR_UNSUPPORTED_DIR_IMPORT ,
51
53
ERR_UNSUPPORTED_ESM_URL_SCHEME ,
52
54
} = require ( 'internal/errors' ) . codes ;
53
55
@@ -270,10 +272,15 @@ function finalizeResolution(resolved, base) {
270
272
resolved . pathname , fileURLToPath ( base ) , 'module' ) ;
271
273
}
272
274
273
- if ( StringPrototypeEndsWith ( resolved . pathname , '/' ) ) return resolved ;
274
275
const path = fileURLToPath ( resolved ) ;
275
-
276
- if ( ! tryStatSync ( path ) . isFile ( ) ) {
276
+ const stats = tryStatSync ( path ) ;
277
+
278
+ if ( stats . isDirectory ( ) ) {
279
+ const err = new ERR_UNSUPPORTED_DIR_IMPORT (
280
+ path || resolved . pathname , fileURLToPath ( base ) ) ;
281
+ err . url = String ( resolved ) ;
282
+ throw err ;
283
+ } else if ( ! stats . isFile ( ) ) {
277
284
throw new ERR_MODULE_NOT_FOUND (
278
285
path || resolved . pathname , fileURLToPath ( base ) , 'module' ) ;
279
286
}
@@ -749,7 +756,8 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
749
756
} catch ( error ) {
750
757
// Try to give the user a hint of what would have been the
751
758
// resolved CommonJS module
752
- if ( error . code === 'ERR_MODULE_NOT_FOUND' ) {
759
+ if ( error . code === 'ERR_MODULE_NOT_FOUND' ||
760
+ error . code === 'ERR_UNSUPPORTED_DIR_IMPORT' ) {
753
761
const found = resolveAsCommonJS ( specifier , parentURL ) ;
754
762
if ( found ) {
755
763
// Modify the stack and message string to include the hint
0 commit comments