@@ -26,10 +26,6 @@ const assert = require('internal/assert');
2626const internalFS = require ( 'internal/fs/utils' ) ;
2727const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
2828const {
29- closeSync,
30- fstatSync,
31- openSync,
32- readFileSync,
3329 realpathSync,
3430 statSync,
3531 Stats,
@@ -53,6 +49,7 @@ const {
5349 ERR_UNSUPPORTED_ESM_URL_SCHEME ,
5450} = require ( 'internal/errors' ) . codes ;
5551
52+ const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
5653const DEFAULT_CONDITIONS = ObjectFreeze ( [ 'node' , 'import' ] ) ;
5754const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
5855
@@ -78,37 +75,25 @@ function tryStatSync(path) {
7875 }
7976}
8077
81- function readIfFile ( path ) {
82- let fd ;
83- try {
84- fd = openSync ( path , 'r' ) ;
85- } catch {
86- return undefined ;
87- }
88- try {
89- if ( ! fstatSync ( fd ) . isFile ( ) ) return undefined ;
90- return readFileSync ( fd , 'utf8' ) ;
91- } finally {
92- closeSync ( fd ) ;
93- }
78+ /**
79+ *
80+ * '/foo/package.json' -> '/foo'
81+ */
82+ function removePackageJsonFromPath ( path ) {
83+ return StringPrototypeSlice ( path , 0 , path . length - 13 ) ;
9484}
9585
96- function getPackageConfig ( path , base ) {
86+ function getPackageConfig ( path ) {
9787 const existing = packageJSONCache . get ( path ) ;
9888 if ( existing !== undefined ) {
99- if ( ! existing . isValid ) {
100- throw new ERR_INVALID_PACKAGE_CONFIG ( path , fileURLToPath ( base ) , false ) ;
101- }
10289 return existing ;
10390 }
104-
105- const source = readIfFile ( path ) ;
91+ const source = packageJsonReader . read ( path ) . string ;
10692 if ( source === undefined ) {
10793 const packageConfig = {
10894 exists : false ,
10995 main : undefined ,
11096 name : undefined ,
111- isValid : true ,
11297 type : 'none' ,
11398 exports : undefined
11499 } ;
@@ -119,17 +104,9 @@ function getPackageConfig(path, base) {
119104 let packageJSON ;
120105 try {
121106 packageJSON = JSONParse ( source ) ;
122- } catch {
123- const packageConfig = {
124- exists : true ,
125- main : undefined ,
126- name : undefined ,
127- isValid : false ,
128- type : 'none' ,
129- exports : undefined
130- } ;
131- packageJSONCache . set ( path , packageConfig ) ;
132- return packageConfig ;
107+ } catch ( error ) {
108+ const errorPath = removePackageJsonFromPath ( path ) ;
109+ throw new ERR_INVALID_PACKAGE_CONFIG ( errorPath , error . message , true ) ;
133110 }
134111
135112 let { main, name, type } = packageJSON ;
@@ -143,7 +120,6 @@ function getPackageConfig(path, base) {
143120 exists : true ,
144121 main,
145122 name,
146- isValid : true ,
147123 type,
148124 exports
149125 } ;
@@ -171,7 +147,6 @@ function getPackageScopeConfig(resolved, base) {
171147 exists : false ,
172148 main : undefined ,
173149 name : undefined ,
174- isValid : true ,
175150 type : 'none' ,
176151 exports : undefined
177152 } ;
@@ -590,8 +565,7 @@ function packageResolve(specifier, base, conditions) {
590565 let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
591566 let lastPath ;
592567 do {
593- const stat = tryStatSync (
594- StringPrototypeSlice ( packageJSONPath , 0 , packageJSONPath . length - 13 ) ) ;
568+ const stat = tryStatSync ( removePackageJsonFromPath ( packageJSONPath ) ) ;
595569 if ( ! stat . isDirectory ( ) ) {
596570 lastPath = packageJSONPath ;
597571 packageJSONUrl = new URL ( ( isScoped ?
0 commit comments