help
help copied to clipboard
Add max-old-space-size to npmrc
- Node.js Version: v11.10.0
- OS: MAC OS X 10.14.3
- Scope (install, code, runtime, meta, other?): executing any package.json script
I need to set --max-old-space-size on .npmrc to increase the memory limit like this:
node-options="--max-old-space-size=4096"
I don't understand why but doesn't works, maybe I did a mistake but node report shows the variable under the environmentVariables
section like this:
"npm_config_node_options": "--max-old-space-size=4096",
Any idea?
Thanks!
Hi @rafa-as 😃 use one of these methods.
In command line use this:
node --max-old-space-size=yourSize yourFile.js
wherein yourSize change to your specific size.
--------------- or--------------
You can custom a script, go to package.json and set:
"scripts": {
"size": "node --max-old-space-size=254"
}
Use command npm run size yourFile.js
I hope you were helpful 😄
Thank you @aymen94.
I know those methods but I want to set it globally for a project.
@rafa-as With this in your package.json :
{
// other configuration in package.json
"scripts":{
"start": "node --max-old-space-size=4076 main.js"
}
}
and use command to run your project npm start
@aymen94 I have more than 20 scripts in the package.json. None of them executes directly node. This is the reason because I want to set globally in the npmrc.
Thanks anyway
What about NODE_OPTIONS
env variable? It seems it supports --max-old-space-size
.
What about
NODE_OPTIONS
env variable? It seems it supports--max-old-space-size
.
Yes, it supports, but should be added to each script or add to every environment, breaking the self-contained project principle. This is the reason because I'm asking about npmrc that can be placed and commited inside the project.
@rafa-as - looks like it is difficult to achieve your requirement.
can you elaborate on the self-contained project principle
? does it mean each project is an independently configurable and runnable software entity? If that is the case, controlling the behavior from a central place does not seem meaningful and fair? I may be missing something, so an illustration of the scenario will help!
@gireeshpunathil thanks for your response.
does it mean each project is an independently configurable and runnable software entity?
Yes, that's it.
If that is the case, controlling the behavior from a central place does not seem meaningful and fair?
In my last comment I wrote: "This is the reason because I'm asking about npmrc that can be placed and commited inside the project." .
We have a lot of projects with one different npmrc in each project, for example to change the npm registry. At this point you can see the control is given to every particular project not in a central place like an env variable.
Another reason is to avoid the noise caused by the --max-old-space-size repetition on every script inside package.json.
When you said looks like it is difficult to achieve your requirement.
Did you mean technical or conceptual difficulties?
I hope that this explanations solves your doubts about what I mean with self-contained project principle
and clarify my requirement.
Thanks another time @gireeshpunathil
@rafa-as - thanks. I meant conceptual difficulties: the effort for placing the memory flag, and the noise that is caused by its presence in npmrc - are they in any ways lighter than the effort / noise that are associated with the scripts?
ping @rafa-as
Sorry @gireeshpunathil I was on holidays.
I think that put one flag in one place it's lighter than put it on every script, 1 - n relation. Also in my opinion, it's more maintainable.
What do you think @gireeshpunathil ?
@rafa-as - agree that putting in one place is more convenient, and that is what is NODE_OPTIONS
. But looks like you are not comfortable with that option.
Unfortunately, I dont have any additional suggestions!
Imagine this case:
- 4 projects, 35 scripts each project in a single monorepository.
- The 35 scripts needs the
NODE_OPTIONS
flag.
Actually you have two options:
- Add
NODE_OPTIONS=--max-old-space-size=4096
on every single script -> Result: 35 flags - Create one script that sets
NODE_OPTIONS=--max-old-space-size=4096
and call it on every single script -> Result: 1 flag + 35 calls to the script that sets the flag.
My approach:
Add max-old-space-size=4096
to project .npmrc -> Result: 1 flag and enjoy
Pros:
- Less code
- More maintainable
- Less confusion in scripts
- All node configurations in the correct place and centralised
- Overwrite in a particular case it's possible with the env variable
Cons:
- Nothing
+1 this is will be very useful for big angular apps (https://github.com/angular/angular-cli/issues/13734)
Something have changed. Works now.
$ npm run ttt
> node -i -e "console.log('NODE_OPTIONS =', process.env.NODE_OPTIONS); process.exit();"
> NODE_OPTIONS = undefined
$ echo "node-options=\"--max-old-space-size=4096\"" >> ./.npmrc
$ npm run ttt
> node -i -e "console.log('NODE_OPTIONS =', process.env.NODE_OPTIONS); process.exit();"
> NODE_OPTIONS = --max-old-space-size=4096
$ node -v
v10.16.3
$ npm -v
6.9.0
Probably, now it's documentation only bug.
Thanks, @l0ki000
I'll check this week
Did it work? @rafa-as
@l0ki000 @remiX- unfortunately doesn't work for us.
Maybe is the node version
Ok neither but I'm on 8.9.2 still... Will try 10.x later today :)
Just open your terminal and type : set NODE_OPTIONS=--max-old-space-size=4096 and then press enter. This will solve all your problems!! All the best!!
@Malik-Jehangir if you read the thread properly, you can see that we already know your "solution" but we are searching for a more elegant, scalable and especially a "self-contained project" solution.
Thanks for your effort but is not the desired solution.
I can confirm using .npmrc
works on 10.17.0
:
# .npmrc
node-options=--max_old_space_size=4096
I can confirm using
.npmrc
works on10.17.0
:# .npmrc node-options=--max_old_space_size=4096
@noslouch can you give us more details about your test please?
sure. here's a test project...
# /tmp/test
-rw-r--r-- 1 noslouch wheel 39B Feb 5 12:21 .npmrc
-rw-r--r-- 1 noslouch wheel 38B Feb 5 12:21 index.js
-rw-r--r-- 1 noslouch wheel 171B Feb 5 12:21 package.json
# .npmrc
node-options=--max_old_space_size=4096
// index.js
console.log(process.env.NODE_OPTIONS)
// package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC"
}
and when I run npm start
from the command line, I see:
> [email protected] start /private/tmp/test
> node index.js
--max_old_space_size=4096
thanks @noslouch we will try another time in our projects today.
Time ago we was tried but when the scripts require more memory raises an error like FATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memory
.
@noslouch, unfortunately, doesn't work.
I can see the flag in the error report: "npm_config_node_options": "--max_old_space_size=4096"
but script execution also raise an exeption.
I've tested with yarn and npm and I have obtained the following results:
NPM: npm run
Case 1
/
-- .npmrc
-- package.json
-- projects
|-- angular
|-- package.json <---- script executed
Result: doesn't work because doesn't get properly the npmrc options. Seems to be a node related bug
Case 2
/
-- .npmrc
-- package.json
-- projects
|-- angular
|-- .npmrc
|-- package.json <---- script executed
Result: works properly
YARN: yarn
Case 1
/
-- .npmrc
-- .yarnrc
-- package.json
-- projects
|-- angular
|-- package.json <---- script executed
Result: doesn't work because doesn't get properly the npmrc options. Seems to be a yarn related bug
Case 2
/
-- .npmrc
-- .yarnrc
-- package.json
-- projects
|-- angular
|-- .npmrc
|-- .yarnrc
|-- package.json <---- script executed
Result: doesn't work because doesn't get properly the npmrc options. Seems to be a yarn related bug
CONCLUSION: yarn and node both have a bug.
- node doesn't read properly npmrc following the folders structure when found a package.json
- yarn doesn't work in no case
@rafa-as thank you for persisting in the face of everyone suggesting the same suboptimal solutions over and over again. This is a much-needed feature!
Bump, keeping track of this. Thanks @rafa-as
@rafa-as how is the config set up in your .npmrc and .yarnrc?
.npmrc
registry=https://registry.****.com/repository/yarn/
always-auth=true
node-options=--max_old_space_size=4096
.yarnrc
no-progress true
registry "https://registry.****.com/repository/yarn/"
node-options --max_old_space_size=4096
Anyway, the important part is the folder structure, we have the following structure:
/
-- .npmrc
-- .yarnrc
-- package.json
-- projects
|-- angular
|-- package.json <---- script executed
We are using yarn workspaces
Another year has passed. Does anyone have a working solution with the .npmrc file?