create-react-app设置max_old_space_size无效,触发语法错误
Hey there, let's get this sorted out! The issue you're hitting happens because react-scripts v1.x doesn't recognize the --max_old_space_size parameter when you append it directly to the start/build commands—it tries to parse it as a react-scripts flag, which causes the syntax error. Here's how to fix it properly for your setup:
1. Directly Target the React Scripts Entry Files
Instead of passing the flag to react-scripts start, run the underlying Node script directly with the memory flag. Update your package.json scripts like this:
"scripts": { "start": "node --max_old_space_size=4096 node_modules/react-scripts/scripts/start.js", "build": "node --max_old_space_size=4096 node_modules/react-scripts/scripts/build.js" }
This way, the memory flag is passed directly to Node, not to react-scripts, so it won't trigger a syntax error.
2. Use Environment Variables (Windows-Specific)
If you prefer not to modify the script paths, set the NODE_OPTIONS environment variable before running the command:
- For Command Prompt:
set NODE_OPTIONS=--max_old_space_size=4096 && yarn start - For PowerShell:
$env:NODE_OPTIONS="--max_old_space_size=4096"; yarn start
You can also embed this directly in your package.json scripts (Windows-only):
"scripts": { "start": "set NODE_OPTIONS=--max_old_space_size=4096 && react-scripts start", "build": "set NODE_OPTIONS=--max_old_space_size=4096 && react-scripts build" }
3. Clean Up Caches (Just to Be Safe)
Since you already reinstalled node_modules, let's make sure no stale cache is causing issues:
yarn cache clean yarn install
Why Your Original Approach Failed
React-scripts v1.1.4 doesn't forward arbitrary Node flags to the underlying Node process when you append them to react-scripts start/build. It only accepts its own supported flags (like --verbose or --no-open), so passing --max_old_space_size confuses it and throws a syntax error.
内容的提问来源于stack exchange,提问作者bytecode

