在Angular 7应用程序中,当我执行ng serve命令时,它将引发“ JavaScript堆内存不足”错误。
从不同的SO答案中,我可以了解到该问题是由于没有足够的内存分配给Node所致。如他们的回答所述,通过执行以下命令,我可以编译/构建angular应用程序。
node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng serve
但是我需要将此内存分配代码写在package.json或某个配置文件中,这样我就不必每次都执行整个命令。还期望代码逻辑应适用于所有环境,并且在较高环境中的构建过程中不应引起任何问题。
有人可以帮我解决这个问题吗?下面是我的package.json代码。
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.config.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
答案 0 :(得分:2)
第一次运行:
npm install -g increase-memory-limit
然后将此行添加到您的 package.json 文件中:
"myCustomMemoryComm ": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng"
赞:
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.config.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"myCustomMemoryComm": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng"
},
然后,您的进一步命令将每次用于运行项目(服务):
npm run myCustomMemoryComm -serve
或直接在控制台上运行:
node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng
对于生产版本:
node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --baseHref=/your-project-name/ --prod=true