File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed
packages/react-scripts/config Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -123,7 +123,39 @@ function getModules() {
123
123
// Otherwise we'll check if there is jsconfig.json
124
124
// for non TS projects.
125
125
} else if ( hasJsConfig ) {
126
- config = require ( paths . appJsConfig ) ;
126
+ // Prefer using TypeScript parser to support JSON with comments in jsconfig.json
127
+ let ts ;
128
+ try {
129
+ ts = require (
130
+ resolve . sync ( 'typescript' , {
131
+ basedir : paths . appNodeModules ,
132
+ } )
133
+ ) ;
134
+ } catch ( e ) {
135
+ ts = null ;
136
+ }
137
+
138
+ if ( ts ) {
139
+ // Use TypeScript's tolerant JSON parser which supports comments (JSONC)
140
+ config = ts . readConfigFile ( paths . appJsConfig , ts . sys . readFile ) . config ;
141
+ } else {
142
+ // Fallback: parse as strict JSON and provide actionable error for JSONC
143
+ try {
144
+ const fileContent = fs . readFileSync ( paths . appJsConfig , 'utf8' ) ;
145
+ config = JSON . parse ( fileContent ) ;
146
+ } catch ( e ) {
147
+ throw new Error (
148
+ chalk . red . bold (
149
+ 'Failed to parse jsconfig.json.\n' +
150
+ 'If your jsconfig.json contains comments, please install TypeScript in your project:\n' +
151
+ ' npm install --save-dev typescript\n' +
152
+ 'or\n' +
153
+ ' yarn add --dev typescript\n' +
154
+ 'Create React App will then parse jsconfig.json with comment support.'
155
+ )
156
+ ) ;
157
+ }
158
+ }
127
159
}
128
160
129
161
config = config || { } ;
You can’t perform that action at this time.
0 commit comments