npm build gives “Ineffective mark-compacts near heap limit Allocation failed”

前端 未结 7 1150
萌比男神i
萌比男神i 2021-02-05 08:44

I have a reactjs/typescript project, running on windows 10. Im trying to build by ts-scripts with

\"rimraf ../wwwroot/* && react-scripts-ts build

相关标签:
7条回答
  • 2021-02-05 08:49

    I don't think that the solution is to increase memory limit. This way the issue is always postponed.

    I've encounter this issue 2 times. I've solved, for each time:

    1. using a specific version of node (v12.11.1, react v16.13.1 and react-scripts v3.2.0);
    2. switching from npm to yarn (node v12.11.1, react v16.13.1 and react-scripts v3.4.1).

    In addition, check your package.json, delete package-lock.json, remove node-modules folder and try to install your dependencies again.

    Next try to build your project.

    0 讨论(0)
  • 2021-02-05 08:54

    The solution which I used was to upgrade the node js version from v10 to v12.


    The command export NODE_OPTIONS=--max_old_space_size=4096 works but has limitations, e.g. you should have ample memory on your machine to run the production build.

    0 讨论(0)
  • 2021-02-05 08:55

    The root cause of the problem is the large source file sizes.

    You can resolve the issue in 2 steps.

    1. First try to increase space size and check the application. If the problem is cause by your source code, then this can be resolved by increasing the Node Heap Size as below,
    "start": "react-scripts --max_old_space_size=4096 start",
    "build": "react-scripts --max_old_space_size=4096 build"
    
    1. Second step is setting the Environment Variable. If the problem lies within your dependencies, you may need to increase the Node Heap allocation globally to resolve the issue.
      For Windows: set NODE_OPTIONS=--max_old_space_size=4096
      For Linux: export NODE_OPTIONS=--max_old_space_size=4096

    Also note that, if you are using very low memory like 512MB, then you will have to upgrade it to at least 2GB to get the better result.

    Here I used 4096MB = 4GB size, but you can change this according to your machine configuration specifically RAM. You can set it to 2GB or 1GB and check the application.

    0 讨论(0)
  • 2021-02-05 09:00

    I got a fix after increasing the node max_old_space. Here is the fix.

    Instead of running npm start or ng start. Run the below command

    node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve
    

    OR

    You can add the command in package.json file as well

    "start": "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve"
    
    0 讨论(0)
  • 2021-02-05 09:05

    I had the same issue on Windows. The solution was to set the NODE_OPTIONS variable before running npm run build. To set this variable, simply run set NODE_OPTIONS=--max_old_space_size=8192

    0 讨论(0)
  • 2021-02-05 09:09

    I had a similar issue, although on a Mac computer. Setting an environment variable did the trick for me. That way, your node process will take the value and use it independently of which script you're running.

    export NODE_OPTIONS=--max_old_space_size=4096
    

    The run your command again. Good Luck!

    0 讨论(0)
提交回复
热议问题