@@ -212,16 +212,17 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
212212//   -> a.<ext> 
213213//   -> a/index.<ext> 
214214
215- // Check if the directory is a package.json dir. 
216- const  packageMainCache  =  Object . create ( null ) ; 
217- // Explicit exports from package.json files 
218- const  packageExportsCache  =  new  SafeMap ( ) ; 
215+ const  packageJsonCache  =  new  SafeMap ( ) ; 
219216
220- function  readPackageRaw ( requestPath )  { 
217+ function  readPackage ( requestPath )  { 
221218  const  jsonPath  =  path . resolve ( requestPath ,  'package.json' ) ; 
222-   const  json  =  internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ; 
223219
220+   const  existing  =  packageJsonCache . get ( jsonPath ) ; 
221+   if  ( existing  !==  undefined )  return  existing ; 
222+ 
223+   const  json  =  internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ; 
224224  if  ( json  ===  undefined )  { 
225+     packageJsonCache . set ( jsonPath ,  false ) ; 
225226    return  false ; 
226227  } 
227228
@@ -232,45 +233,47 @@ function readPackageRaw(requestPath) {
232233
233234  try  { 
234235    const  parsed  =  JSON . parse ( json ) ; 
235-     packageMainCache [ requestPath ]  =  parsed . main ; 
236-     if  ( experimentalExports )  { 
237-       packageExportsCache . set ( requestPath ,  parsed . exports ) ; 
238-     } 
239-     return  parsed ; 
236+     const  filtered  =  { 
237+       main : parsed . main , 
238+       exports : parsed . exports , 
239+       type : parsed . type 
240+     } ; 
241+     packageJsonCache . set ( jsonPath ,  filtered ) ; 
242+     return  filtered ; 
240243  }  catch  ( e )  { 
241244    e . path  =  jsonPath ; 
242245    e . message  =  'Error parsing '  +  jsonPath  +  ': '  +  e . message ; 
243246    throw  e ; 
244247  } 
245248} 
246249
247- function  readPackage ( requestPath )  { 
248-   const  entry  =  packageMainCache [ requestPath ] ; 
249-   if  ( entry ) 
250-     return  entry ; 
251- 
252-   const  pkg  =  readPackageRaw ( requestPath ) ; 
253-   if  ( pkg  ===  false )  return  false ; 
254- 
255-   return  pkg . main ; 
256- } 
257- 
258- function  readExports ( requestPath )  { 
259-   if  ( packageExportsCache . has ( requestPath ) )  { 
260-     return  packageExportsCache . get ( requestPath ) ; 
250+ function  readPackageScope ( checkPath )  { 
251+   const  rootSeparatorIndex  =  checkPath . indexOf ( path . sep ) ; 
252+   let  separatorIndex ; 
253+   while  ( 
254+     ( separatorIndex  =  checkPath . lastIndexOf ( path . sep ) )  >  rootSeparatorIndex 
255+   )  { 
256+     checkPath  =  checkPath . slice ( 0 ,  separatorIndex ) ; 
257+     if  ( checkPath . endsWith ( path . sep  +  'node_modules' ) ) 
258+       return  false ; 
259+     const  pjson  =  readPackage ( checkPath ) ; 
260+     if  ( pjson )  return  pjson ; 
261261  } 
262+   return  false ; 
263+ } 
262264
263-   const  pkg  =  readPackageRaw ( requestPath ) ; 
264-   if  ( ! pkg )  { 
265-     packageExportsCache . set ( requestPath ,  null ) ; 
266-     return  null ; 
267-   } 
265+ function  readPackageMain ( requestPath )  { 
266+   const  pkg  =  readPackage ( requestPath ) ; 
267+   return  pkg  ? pkg . main  : undefined ; 
268+ } 
268269
269-   return  pkg . exports ; 
270+ function  readPackageExports ( requestPath )  { 
271+   const  pkg  =  readPackage ( requestPath ) ; 
272+   return  pkg  ? pkg . exports  : undefined ; 
270273} 
271274
272275function  tryPackage ( requestPath ,  exts ,  isMain ,  originalPath )  { 
273-   const  pkg  =  readPackage ( requestPath ) ; 
276+   const  pkg  =  readPackageMain ( requestPath ) ; 
274277
275278  if  ( ! pkg )  { 
276279    return  tryExtensions ( path . resolve ( requestPath ,  'index' ) ,  exts ,  isMain ) ; 
@@ -371,7 +374,7 @@ function resolveExports(nmPath, request, absoluteRequest) {
371374    } 
372375
373376    const  basePath  =  path . resolve ( nmPath ,  name ) ; 
374-     const  pkgExports  =  readExports ( basePath ) ; 
377+     const  pkgExports  =  readPackageExports ( basePath ) ; 
375378    const  mappingKey  =  `.${ expansion }  ; 
376379
377380    if  ( typeof  pkgExports  ===  'object'  &&  pkgExports  !==  null )  { 
@@ -947,6 +950,12 @@ Module.prototype._compile = function(content, filename) {
947950
948951// Native extension for .js 
949952Module . _extensions [ '.js' ]  =  function ( module ,  filename )  { 
953+   if  ( filename . endsWith ( '.js' ) )  { 
954+     const  pkg  =  readPackageScope ( filename ) ; 
955+     if  ( pkg  &&  pkg . type  ===  'module' )  { 
956+       throw  new  ERR_REQUIRE_ESM ( filename ) ; 
957+     } 
958+   } 
950959  const  content  =  fs . readFileSync ( filename ,  'utf8' ) ; 
951960  module . _compile ( content ,  filename ) ; 
952961} ; 
0 commit comments