help icon indicating copy to clipboard operation
help copied to clipboard

Add max-old-space-size to npmrc

Open rafa-suagu opened this issue 5 years ago • 33 comments

  • 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!

rafa-suagu avatar Feb 20 '19 09:02 rafa-suagu

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 😄

aymen94 avatar Feb 20 '19 20:02 aymen94

Thank you @aymen94.

I know those methods but I want to set it globally for a project.

rafa-suagu avatar Feb 21 '19 07:02 rafa-suagu

@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 avatar Feb 21 '19 09:02 aymen94

@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

rafa-suagu avatar Feb 21 '19 12:02 rafa-suagu

What about NODE_OPTIONS env variable? It seems it supports --max-old-space-size.

vsemozhetbyt avatar Feb 21 '19 13:02 vsemozhetbyt

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-suagu avatar Feb 21 '19 14:02 rafa-suagu

@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 avatar Jun 19 '19 06:06 gireeshpunathil

@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-suagu avatar Jun 19 '19 14:06 rafa-suagu

@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?

gireeshpunathil avatar Jun 19 '19 15:06 gireeshpunathil

ping @rafa-as

gireeshpunathil avatar Jul 13 '19 06:07 gireeshpunathil

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-suagu avatar Aug 06 '19 07:08 rafa-suagu

@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!

gireeshpunathil avatar Aug 06 '19 07:08 gireeshpunathil

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:

  1. Add NODE_OPTIONS=--max-old-space-size=4096 on every single script -> Result: 35 flags
  2. 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

rafa-suagu avatar Aug 06 '19 07:08 rafa-suagu

+1 this is will be very useful for big angular apps (https://github.com/angular/angular-cli/issues/13734)

LastDragon-ru avatar Sep 04 '19 12:09 LastDragon-ru

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.

l0ki000 avatar Oct 24 '19 11:10 l0ki000

Thanks, @l0ki000

I'll check this week

rafa-suagu avatar Oct 29 '19 14:10 rafa-suagu

Did it work? @rafa-as

remiX- avatar Jan 20 '20 07:01 remiX-

@l0ki000 @remiX- unfortunately doesn't work for us.

Maybe is the node version

rafa-suagu avatar Jan 20 '20 08:01 rafa-suagu

Ok neither but I'm on 8.9.2 still... Will try 10.x later today :)

remiX- avatar Jan 20 '20 08:01 remiX-

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 avatar Jan 22 '20 12:01 Malik-Jehangir

@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.

rafa-suagu avatar Jan 22 '20 13:01 rafa-suagu

I can confirm using .npmrc works on 10.17.0:

# .npmrc
node-options=--max_old_space_size=4096

noslouch avatar Feb 05 '20 16:02 noslouch

I can confirm using .npmrc works on 10.17.0:

# .npmrc
node-options=--max_old_space_size=4096

@noslouch can you give us more details about your test please?

rafa-suagu avatar Feb 05 '20 17:02 rafa-suagu

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

noslouch avatar Feb 05 '20 17:02 noslouch

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.

rafa-suagu avatar Feb 06 '20 09:02 rafa-suagu

@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-suagu avatar Feb 13 '20 11:02 rafa-suagu

@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!

pelotom avatar Mar 04 '20 23:03 pelotom

Bump, keeping track of this. Thanks @rafa-as

raindecastro avatar Mar 25 '20 22:03 raindecastro

@rafa-as how is the config set up in your .npmrc and .yarnrc?

The-Code-Monkey avatar Apr 03 '20 10:04 The-Code-Monkey

.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

rafa-suagu avatar Apr 03 '20 11:04 rafa-suagu

Another year has passed. Does anyone have a working solution with the .npmrc file?

blowsie avatar Aug 19 '22 12:08 blowsie