@@ -213,16 +213,17 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
213213// -> a.<ext>
214214// -> a/index.<ext>
215215
216- // Check if the directory is a package.json dir.
217- const packageMainCache = Object . create ( null ) ;
218- // Explicit exports from package.json files
219- const packageExportsCache = new SafeMap ( ) ;
216+ const packageJsonCache = new SafeMap ( ) ;
220217
221- function readPackageRaw ( requestPath ) {
218+ function readPackage ( requestPath ) {
222219 const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
223- const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
224220
221+ const existing = packageJsonCache . get ( jsonPath ) ;
222+ if ( existing !== undefined ) return existing ;
223+
224+ const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
225225 if ( json === undefined ) {
226+ packageJsonCache . set ( jsonPath , false ) ;
226227 return false ;
227228 }
228229
@@ -233,45 +234,47 @@ function readPackageRaw(requestPath) {
233234
234235 try {
235236 const parsed = JSON . parse ( json ) ;
236- packageMainCache [ requestPath ] = parsed . main ;
237- if ( experimentalExports ) {
238- packageExportsCache . set ( requestPath , parsed . exports ) ;
239- }
240- return parsed ;
237+ const filtered = {
238+ main : parsed . main ,
239+ exports : parsed . exports ,
240+ type : parsed . type
241+ } ;
242+ packageJsonCache . set ( jsonPath , filtered ) ;
243+ return filtered ;
241244 } catch ( e ) {
242245 e . path = jsonPath ;
243246 e . message = 'Error parsing ' + jsonPath + ': ' + e . message ;
244247 throw e ;
245248 }
246249}
247250
248- function readPackage ( requestPath ) {
249- const entry = packageMainCache [ requestPath ] ;
250- if ( entry )
251- return entry ;
252-
253- const pkg = readPackageRaw ( requestPath ) ;
254- if ( pkg === false ) return false ;
255-
256- return pkg . main ;
257- }
258-
259- function readExports ( requestPath ) {
260- if ( packageExportsCache . has ( requestPath ) ) {
261- return packageExportsCache . get ( requestPath ) ;
251+ function readPackageScope ( checkPath ) {
252+ const rootSeparatorIndex = checkPath . indexOf ( path . sep ) ;
253+ let separatorIndex ;
254+ while (
255+ ( separatorIndex = checkPath . lastIndexOf ( path . sep ) ) > rootSeparatorIndex
256+ ) {
257+ checkPath = checkPath . slice ( 0 , separatorIndex ) ;
258+ if ( checkPath . endsWith ( path . sep + 'node_modules' ) )
259+ return false ;
260+ const pjson = readPackage ( checkPath ) ;
261+ if ( pjson ) return pjson ;
262262 }
263+ return false ;
264+ }
263265
264- const pkg = readPackageRaw ( requestPath ) ;
265- if ( ! pkg ) {
266- packageExportsCache . set ( requestPath , null ) ;
267- return null ;
268- }
266+ function readPackageMain ( requestPath ) {
267+ const pkg = readPackage ( requestPath ) ;
268+ return pkg ? pkg . main : undefined ;
269+ }
269270
270- return pkg . exports ;
271+ function readPackageExports ( requestPath ) {
272+ const pkg = readPackage ( requestPath ) ;
273+ return pkg ? pkg . exports : undefined ;
271274}
272275
273276function tryPackage ( requestPath , exts , isMain , originalPath ) {
274- const pkg = readPackage ( requestPath ) ;
277+ const pkg = readPackageMain ( requestPath ) ;
275278
276279 if ( ! pkg ) {
277280 return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -372,7 +375,7 @@ function resolveExports(nmPath, request, absoluteRequest) {
372375 }
373376
374377 const basePath = path . resolve ( nmPath , name ) ;
375- const pkgExports = readExports ( basePath ) ;
378+ const pkgExports = readPackageExports ( basePath ) ;
376379 const mappingKey = `.${ expansion } ` ;
377380
378381 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 ( stripBOM ( content ) , filename ) ;
952961} ;
0 commit comments