@@ -10,7 +10,7 @@ const {
1010} = primordials ;
1111
1212const assert = require ( 'internal/assert' ) ;
13- const { emitExperimentalWarning, getLazy } = require ( 'internal/util' ) ;
13+ const { emitExperimentalWarning, getLazy, setOwnProperty } = require ( 'internal/util' ) ;
1414const { resolve : pathResolve , dirname } = require ( 'path' ) ;
1515const { fileURLToPath } = require ( 'internal/url' ) ;
1616
@@ -211,10 +211,9 @@ function loadPackageMap(configPath) {
211211 try {
212212 content = fs . readFileSync ( configPath ) ;
213213 } catch ( err ) {
214- if ( err . code === 'ENOENT' ) {
215- throw new ERR_PACKAGE_MAP_INVALID ( configPath , 'file not found' ) ;
216- }
217- throw new ERR_PACKAGE_MAP_INVALID ( configPath , err . message ) ;
214+ const error = new ERR_PACKAGE_MAP_INVALID ( configPath , 'failed to read' ) ;
215+ setOwnProperty ( error , 'cause' , err ) ;
216+ throw error ;
218217 }
219218
220219 // Resolve symlinks so that stored paths match the realpath'd module paths
@@ -225,7 +224,9 @@ function loadPackageMap(configPath) {
225224 try {
226225 data = JSONParse ( content ) ;
227226 } catch ( err ) {
228- throw new ERR_PACKAGE_MAP_INVALID ( configPath , `invalid JSON: ${ err . message } ` ) ;
227+ const error = new ERR_PACKAGE_MAP_INVALID ( configPath , `invalid JSON` ) ;
228+ setOwnProperty ( error , 'cause' , err ) ;
229+ throw error ;
229230 }
230231
231232 return new PackageMap ( configPath , data ) ;
@@ -249,6 +250,9 @@ function getPackageMap() {
249250 try {
250251 packageMap = loadPackageMap ( pathResolve ( packageMapPath ) ) ;
251252 } catch ( err ) {
253+ // Fallback to an empty package map to avoid repeatedly trying
254+ // to load the broken package map file. Subsequent resolutions
255+ // will still fail to resolve due to the package map being empty.
252256 packageMap = new PackageMap ( packageMapPath , { packages : { } } ) ;
253257 throw err ;
254258 }
0 commit comments