Skip to content

Commit 2580107

Browse files
committed
feat(react-scripts): support JSON with comments in jsconfig.json (#7426)
1 parent b2a2253 commit 2580107

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/react-scripts/config/modules.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,39 @@ function getModules() {
123123
// Otherwise we'll check if there is jsconfig.json
124124
// for non TS projects.
125125
} 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+
}
127159
}
128160

129161
config = config || {};

0 commit comments

Comments
 (0)