如何增加nodejs的默认内存?

在服务器启动时,将2GB(大约)数据从mongodb导出到redis,然后得到错误为FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

然后用这个命令node --max-old-space-size=4076 server.js启动服务器node --max-old-space-size=4076 server.js并且正常工作。 但需要在nodejs applicaton中进行configuration,以便节点服务器始终以4GB内存启动。 请帮我解决这个问题? 谢谢。

一个选项:npm启动脚本

https://docs.npmjs.com/misc/scripts

这些被添加到你的package.json下的“脚本”部分

 { //other package.json stuff "scripts":{ "start": "node --max-old-space-size=4076 server.js" } } 

然后运行它调用npm start而不是inputnode + args +执行点。

注意:如果您将其命名为start以外的内容, npm run [yourScriptNameHere]将成为运行它的命令

这比尝试重新configuration节点以使用默认的4gb(甚至不知道其可能的tbh)是更好的select。 它使您的configuration可移植,通过使用现有的烘焙方法,并允许其他未来遇到您的代码的人理解这是一个需求。