endel / increase-memory-limit

Increase memory limit for local node binaries ("max-old-space-size")
http://npmjs.com/package/increase-memory-limit
MIT License
269 stars 26 forks source link

Problem in linux platform. #2

Closed javsal closed 6 years ago

javsal commented 7 years ago

I run "increase-memory-limit" command in my project root directory and it changed the first line of all files in /node_modules/.bin/ directory from #!/usr/bin/env to #!/usr/bin/env node --max-old-space-size=10240.

And when I build my angular2 -v4 application for production with AOT it stopped the built and do not through any error.

Normally when I try to build my application its through Heap memory error. I use 64bit Ubuntu 16.04 LTS. with 8GB RAM.

endel commented 7 years ago

Hey @javsal, is this still happening using version 1.0.3?

hoschi commented 7 years ago

I'm also trying this on linux, but for me the task starts and never stops. CPU usage is high as hell and it provides no output. This is the same for all tasks I tested, like eslint, ava, .... Arch Linux 32GB RAM, node version 6.3.1

javsal commented 7 years ago

Is there any way to check the version??

hoschi commented 7 years ago

@javsal npm ls -g | grep increase-memory-limit

fxck commented 7 years ago

Same problem. How can I "uninstall" this?

// edit yea, by deleting node_modules

endel commented 7 years ago

@fxck do you use Angular as well? is this tool breaking all the binaries under Linux platform?

fxck commented 7 years ago

Yea angular, trying to ng build --prod

dawnmist commented 7 years ago

It's breaking every binary for react too. I don't think it matters which framework is being used, just that increase-memory-limit is being used in a linux environment.

endel commented 7 years ago

Apparently on Linux the arguments are not parsed after the node command. The shebang interprets the whole string as a command, which will never be found ("node --max-old-space-size=10240")

#!/usr/bin/env node --max-old-space-size=10240

This hack should do, though. If someone wants to send a pull-request I'd gladly merge.

#!/bin/sh
":" //#; exec /usr/bin/env node --max-old-space-size=10240 "$0" "$@"

Cheers!

dawnmist commented 7 years ago

Thank you @endel - I just found the same bit about the parameter being read as part of the script command and came back to update.

The alternative option is to create a small shell script called 'node' to wrap any call to node and have it earlier in your path than /usr/bin.

e.g. You can use a bin directory in your home directory for personal scripts. To do so, add export PATH=~/bin:${PATH} to your ~/.bashrc or ~/.profile file.

Then create a script file ~/bin/node with the contents

#!/bin/bash
echo "Using `dirname $0` version of node..."
/usr/bin/node --max-old-space-size=10240 $@

The echo line can be commented out with a # at the beginning - I only put it there to confirm that I was getting my overridden script running properly.

Mark the script as executable with chmod +x ~/bin/node. By using the same file name as the node executable and putting the path to your personal scripts directory earlier in your PATH variable than the normal node executable, any time you try to run 'node' you will activate your wrapper script for it.

For the first time you're using it, if you didn't already have a ~/bin directory listed in your path you'll probably need to do export PATH=~/bin:${PATH} in the terminal before trying to run node to add the ~/bin directory to the beginning of your path. You can check if this is needed by typing which node - if this still lists /usr/bin/node in response it means that you need to do the export.

By adding the PATH line to your .bashrc or .profile file, it'll automatically set up your environment to run your wrapper for any new terminal windows or whenever you login in future :). If you want to get rid of your wrapper, simply remove the ~/bin/node file and the system will switch back to using /usr/bin/node automatically.

jcabrerazuniga commented 6 years ago

Any solution to this problem? I am having this problem in Ubuntu 16.04LTS. There I am using the node version installed by nvm. Thx

Martodox commented 6 years ago

Any update on the issue?

rvanmarkus commented 6 years ago

+1

DemitriM commented 6 years ago

I am having the same issue on UbuntuMate. Boooo :(

dawnmist commented 6 years ago

First, I'd like to thank Endel for this package. I don't think this is an issue that can be "fixed" though, since it's a fundemental issue in how shell scripts are processed on a linux machine.

There is an alternative though. If you're working in linux, you can set an environment variable to change the node memory limit. See https://nodejs.org/dist/latest-v8.x/docs/api/cli.html#cli_node_options_options

For example:

export NODE_OPTIONS="--max-old-space-size=8192"
npm run build

To have that set globally on your machine, add it to your .bashrc or .profile file.

If using react, they accept the --max-old-space-size flag directly in their scripts, so you can alter your package.json file like so:

"scripts": {
  "start": "react-scripts --max-old-space-size=8192 start",
  "build": "react-scripts --max-old-space-size=8192 build",
  "test": "react-scripts --max-old-space-size=8192 test"
}

Or you can use something like cross-env to set the command line variable in an OS-independent manner and pass it to any script inside your package.json like:

"scripts": {
  "start": "cross-env NODE_OPTIONS=\"--max_old_space_size=4096\" webpack"
}

Doing the above means that you don't need to use the increase-memory-limit package, and when altering the package.json file the size increase can still be shared between developers/different machines without any issues (something my original workaround of a shell script wrapper couldn't do - each developer had to create it manually on each machine they worked on). The package.json methods also mean that if you're working on multiple projects, each of them can set their own memory limit requirements independently of each other (in their own package.json files).

jcabrerazuniga commented 6 years ago

@dawnmist , I ended following your previous comment about creating a node file inside a bin folder without using the increase memory limit package. This package works in windows but for Linux -Ubuntu- I suggest everybody to follow your previous comment. Now, I can compile with npm and webpack with no problems. In Ubuntu I had to use nvm to install node and change /usr/bin/node with the path of the node file left by nvm. In my case was somehing like:

/home/username/.nvm/versions/node/v9.11.0/bin/node --max-old-space=10240 $@

just a question: what is the $@ for?

thx

dawnmist commented 6 years ago

@jcabrerazuniga The $@ is an environment variable that contains the list of parameters that was passed to the script. So in this case, it means that if you ran node index.js, it would contain the index.js parameter, and by using it in the updated call to node you are passing it on to the real node to handle. https://stackoverflow.com/questions/9994295/what-does-mean-in-a-shell-script

The script essentially rewrites 'node index.js' to '/home/username/.nvm/versions/node/v9.11.0/bin/node --max-old-space=10240 index.js'.

endel commented 6 years ago

Thanks, @dawnmist for your thorough explanation on this.

I'd like to say this library is officially deprecated as of Node.js v8.0+ has been released. Please see the README for the alternative solution: https://github.com/endel/increase-memory-limit#deprecation-notice