@@ -12,6 +12,7 @@ const {
1212 RegExp,
1313 SafeMap,
1414 SafeSet,
15+ String,
1516 StringPrototypeEndsWith,
1617 StringPrototypeIncludes,
1718 StringPrototypeIndexOf,
@@ -48,6 +49,7 @@ const {
4849 ERR_INVALID_PACKAGE_TARGET ,
4950 ERR_MODULE_NOT_FOUND ,
5051 ERR_PACKAGE_PATH_NOT_EXPORTED ,
52+ ERR_UNSUPPORTED_DIR_IMPORT ,
5153 ERR_UNSUPPORTED_ESM_URL_SCHEME ,
5254} = require ( 'internal/errors' ) . codes ;
5355
@@ -270,10 +272,15 @@ function finalizeResolution(resolved, base) {
270272 resolved . pathname , fileURLToPath ( base ) , 'module' ) ;
271273 }
272274
273- if ( StringPrototypeEndsWith ( resolved . pathname , '/' ) ) return resolved ;
274275 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 ( ) ) {
277284 throw new ERR_MODULE_NOT_FOUND (
278285 path || resolved . pathname , fileURLToPath ( base ) , 'module' ) ;
279286 }
@@ -749,7 +756,8 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
749756 } catch ( error ) {
750757 // Try to give the user a hint of what would have been the
751758 // 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' ) {
753761 const found = resolveAsCommonJS ( specifier , parentURL ) ;
754762 if ( found ) {
755763 // Modify the stack and message string to include the hint
0 commit comments