Best way to set --max-old-space-size when running npm? #12238
Comments
There's not a really good way to pass arbitrary parameters to the Node process from the command line; maybe there should be. Pointing this at the team for further discussion in product triage. |
Cool, thanks @othiym23! Also, have you heard of other teams running up against memory limits when installing npm dependencies? Any tips/tricks to reduce our memory footprint? We're going to be going through our packages and trimming out anything that we don't need, but just curious as to how common or uncommon this is. |
It's not super common that this needs to be done, mostly (I think) because most of the time people are doing deploys and installs from fairly beefy vms with the gig or so of RAM that npm needs to run efficiently. When it's an issue, it's generally on Raspberry Pis and other small machines, but it's fairly rare for installs even then – this is mostly an issue with using npm's not-too-beloved search. Also, I'm a little confused by the correlation you've drawn between Either way, being able to pass parameters to the runtime like you suggest is a super handy, if rarely necessary, tool, and it would be nice to have in npm. Unfortunately, Node itself doesn't allow configuration via environment variables, and npm on non-Windows systems is run directly as a Node script, so we'd have to write a new shell script to handle this, which would require some coördination with the Node.js team that's responsible for bundling and installing npm. This is something the npm CLI team is going to try to get to in the next year or so, but if somebody else wants to take a stab at it first, that would be great. Thanks! |
@othiym23 thanks for the writeup, the correlation is definitely odd but something we noticed when running installs against our in-house npm server (sinopia). When the memory would grow beyond 1.7gb the node process would exit and we would see ECONNRESET errors in our build logs. Increasing the limit did seem to make installs more reliable, but this was likely just a coincidence. After more debugging today we're pretty sure this is some sort of networking issue between our internal npm server (sinopia) and our build agents, since like you said the process should just exit immediately if we were overflowing the memory limit. And we've had repeated incidents of ECONNRESET with the newly up'd memory limits. Thanks again for the write-up and adding the feature to your backlog - the work you and the rest the cli team do is greatly appreciated! |
I use aliasing to pass options to node for npm. Here is my .bashrc: alias npm='node --max_old_space_size=8000 /usr/bin/npm' |
@othiym23 I don't think this is an edge use-case. Do I need to hard-code this in my docker image, even though the memory is allocated at runtime? Is there a reason this isn't an environment variable? |
We nee this quite often, when running large things using npm scripts. |
@othiym23 so is it |
nevermind, I found it by RTFM here:
|
Linux only accept one argument in a shebang, so everything after the /usr/bin/env is seen as one argument. For setting this node option, see npm/npm#12238 Resolve ionic-team#838
… giving app-scripts more memory by default Linux only accept one argument in a shebang, so everything after the /usr/bin/env is seen as one argument. For setting this node option, see npm/npm#12238 Resolve #838
There IS an easy way to pass parameter down to node from npm:
|
@maxgalbu As far as I know, that just passes those args down to whatever your |
I had success with:
|
I faced this issue too, and found this npm package helpful increase-memory-limit |
Example to previous answer
|
I would have expected to be able to pass $ npm run start --node-flags --max-old-space-size=512 --no-warnings |
@MichaelJCole Were you able to find out a solution for setting the value in Docker container (at run time) ? |
The package suggested by @EXin it a good workaround for now. I have the feeling this is mostly a problem with webpack or rollup and they might be running on a child process of the actual process started with the npm run command. For that reason, it might be there responsibility to tweak their commands so people don't run into those issues. |
I disagree, maybe the tweaking is necessary too, but I should be able to pass these flags to node. |
It'd be even nicer if there was a ENV_VAR to set this for an entire system |
@HipsterZipster it's not perfect, but you could set it up for yourself. I'm currently doing this, which seems to work well:
|
any news if that is coming? There is one more bash solution https://stackoverflow.com/a/40939496/415078 wonder is there is a way to do something similar on Windows: |
desperately needed here too! cross-platform team running into issues with webpack, especially on windows. would be great to be able to specify max-old-space-size via the npm script |
So it turns out that instead of needing to alias npm or otherwise call node directly, you can increase Node's max heap size by setting the NODE_OPTIONS=--max_old_space_size=4096 Usage with NPM scripts: "scripts": {
"start": "cross-env NODE_OPTIONS=--max_old_space_size=4096 webpack"
}
Note that it's important to specify the option with_underscores since that's the only one that NODE_OPTIONS accepts. |
https://nodejs.org/dist/latest-v8.x/docs/api/cli.html#cli_node_options_options |
It turns out either way should work, see nodejs/node#14093. I didn't see that documentation and was basing my statement on the source code, but I overlooked that either variant is allowed. |
Is there a way to set the I tried setting it to 16GB but it still just uses 8GB before giving up. [EDIT] |
@AnyhowStep I'm interested in how to increase the memory to 32GB too. Did you get any progress about this? BTW: it seems that you have a huge TypeScript project! |
It's not so much huge as it is complicated, I think. I never did figure it out, unfortunately. |
1. ProposalYou can create a .npmrc in your project (see also project-config-file) and define your options there: 2. ProposalYou add a config field in your package.json and define your options there. I'll try out both of this myself. I hope they work, since these would a much cleaner approach instead of writing "node ..." in front of every npm script... EditBoth does not work. I couldn't find any solution to set this value within project configurations. There is neither the possibility to run a wildcard script before any npm script. Either you have to set the user environment variable (which you can't access for good reasons within npm) or you have to insert it in all npm scripts, which is quite ugly... |
We have some pretty large builds at Hudl, and we've been seeing
npm install
fail withECONNRESET
errors. We spent a lot of time debugging the issue and are fairly confident the failures were due to npm hitting the upper limit of memory within node and being terminated prematurely.We changed our npm.cmd file to set
--max-old-space-size
to 4gb when starting npm, and this fixed our issue, but we're wondering if there is a better way to set this parameter when running npm?The text was updated successfully, but these errors were encountered: