@@ -213,16 +213,17 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
213
213
// -> a.<ext>
214
214
// -> a/index.<ext>
215
215
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 ( ) ;
220
217
221
- function readPackageRaw ( requestPath ) {
218
+ function readPackage ( requestPath ) {
222
219
const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
223
- const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
224
220
221
+ const existing = packageJsonCache . get ( jsonPath ) ;
222
+ if ( existing !== undefined ) return existing ;
223
+
224
+ const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
225
225
if ( json === undefined ) {
226
+ packageJsonCache . set ( jsonPath , false ) ;
226
227
return false ;
227
228
}
228
229
@@ -233,45 +234,47 @@ function readPackageRaw(requestPath) {
233
234
234
235
try {
235
236
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 ;
241
244
} catch ( e ) {
242
245
e . path = jsonPath ;
243
246
e . message = 'Error parsing ' + jsonPath + ': ' + e . message ;
244
247
throw e ;
245
248
}
246
249
}
247
250
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 ;
262
262
}
263
+ return false ;
264
+ }
263
265
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
+ }
269
270
270
- return pkg . exports ;
271
+ function readPackageExports ( requestPath ) {
272
+ const pkg = readPackage ( requestPath ) ;
273
+ return pkg ? pkg . exports : undefined ;
271
274
}
272
275
273
276
function tryPackage ( requestPath , exts , isMain , originalPath ) {
274
- const pkg = readPackage ( requestPath ) ;
277
+ const pkg = readPackageMain ( requestPath ) ;
275
278
276
279
if ( ! pkg ) {
277
280
return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -372,7 +375,7 @@ function resolveExports(nmPath, request, absoluteRequest) {
372
375
}
373
376
374
377
const basePath = path . resolve ( nmPath , name ) ;
375
- const pkgExports = readExports ( basePath ) ;
378
+ const pkgExports = readPackageExports ( basePath ) ;
376
379
const mappingKey = `.${ expansion } ` ;
377
380
378
381
if ( typeof pkgExports === 'object' && pkgExports !== null ) {
@@ -947,6 +950,12 @@ Module.prototype._compile = function(content, filename) {
947
950
948
951
// Native extension for .js
949
952
Module . _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
+ }
950
959
const content = fs . readFileSync ( filename , 'utf8' ) ;
951
960
module . _compile ( stripBOM ( content ) , filename ) ;
952
961
} ;
0 commit comments