Why Am I Seeing the Error Message "JavaScript heap out of memory"?
Symptoms
When the npm build task is executed, the following error information is displayed in the log.
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.
Cause Analysis
By default, the Node.js runtime caps memory usage at around 1.4 GB on 64-bit systems and 0.7 GB on 32-bit systems. Your build blows past that ceiling, so you hit an out-of-memory wall.
Solution
Method 1: Upgrade the Node.js version. Set the tool version by referring to Building with npm.
Method 2: Set the --max_old_space_size or --max_new_space_size parameter when starting the node to adjust the memory usage limit.
node --max_old_space_size=1700 test.js // The unit is MB. Modifies the old memory limit. node --max_new_space_size=1024 test.js // The unit is KB. Modifies the new memory limit.
The solutions for the three major front-end frameworks are described in the following table.
|
Frame |
Solution |
|---|---|
|
Vue |
Change the value of build in the package.json file and include the node command along with its parameters. For example: "build": "node --max_old_space_size=4096 ./node_modules/vite/bin/vite.js build" or "build": "node --max_old_space_size=4096 ./node_modules/@vue/cli-service/bin/vue-cli-service.js build" |
|
React |
The scripts field in the package.json file is as follows: "scripts": {
"start": "react-scripts start", # Start the development server and run the React application.
"build": "react-scripts build", # Build code for the production environment.
"test": "react-scripts test --env=jsdom", # Run the test script.
"eject": "react-scripts eject" # Decouple the project from the create-react-app scaffold.
}
When you run npm run build, it essentially executes react-scripts build. Open the project's root directory, locate the node_modules folder, and find the .bin directory. Open the react-scripts file in the directory, and append --max_old_space_size=4096 to #!/usr/bin/env node. #!/usr/bin/env node --max_old_space_size=4096 |
|
Angular |
The scripts field in the package.json file is as follows: "scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}
Just like with React's scripts, Angular's ng file also lives in the .bin directory under the node_modules folder of your project. Open that file and edit the first line. #!/usr/bin/env node --max_old_space_size=4096 |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot