├── .gitignore ├── package.json ├── LICENSE ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "increase-memory-limit", 3 | "version": "1.0.7", 4 | "description": "Increase memory limit for local node binaries ('max-old-space-size')", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "bin": { 10 | "increase-memory-limit": "index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/endel/increase-memory-limit.git" 15 | }, 16 | "publishConfig": { 17 | "registry": "https://registry.npmjs.org/" 18 | }, 19 | "author": "Endel Dreyer", 20 | "license": "ISC", 21 | "dependencies": { 22 | "glob": "^7.1.1" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Endel Dreyer 2 | 3 | MIT License: 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATION NOTICE 2 | 3 | As of Node.js v8.0 shipped August 2017, you can now use the `NODE_OPTIONS` 4 | environment variable to set the max_old_space_size globally. ([#19](https://github.com/endel/increase-memory-limit/issues/19)) 5 | 6 | ``` 7 | export NODE_OPTIONS=--max_old_space_size=4096 8 | ``` 9 | 10 | increase-memory-limit 11 | === 12 | 13 | Workaround to fix `heap out of memory` when running node binaries. It's a common 14 | issue when using TypeScript 2.1+ and webpack. 15 | 16 | This tool will append `--max-old-space-size=4096` in all `node` calls inside 17 | your `node_modules/.bin/*` files. 18 | 19 | ``` 20 | FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 21 | ``` 22 | 23 | How to use 24 | --- 25 | 26 | ``` 27 | npm install -g increase-memory-limit 28 | ``` 29 | 30 | Run from the root location of your project: 31 | 32 | ``` 33 | increase-memory-limit 34 | ``` 35 | 36 | Running from NPM task 37 | --- 38 | 39 | Alternatively, you can configure a npm task to run the fix. 40 | 41 | ```javascript 42 | // ... 43 | "scripts": { 44 | "fix-memory-limit": "cross-env LIMIT=2048 increase-memory-limit" 45 | }, 46 | "devDependencies": { 47 | "increase-memory-limit": "^1.0.3", 48 | "cross-env": "^5.0.5" 49 | } 50 | // ... 51 | ``` 52 | 53 | ``` 54 | npm run fix-memory-limit 55 | ``` 56 | 57 | License 58 | --- 59 | 60 | MIT 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const path = require('path'); 3 | const glob = require('glob'); 4 | const fs = require('fs'); 5 | 6 | const maxOldSpaceSize = process.env.LIMIT || 10240; 7 | const cwd = process.cwd() + path.sep; 8 | 9 | glob(path.join(cwd, "node_modules", ".bin", "*"), function (err, files) { 10 | 11 | files.forEach(file => { 12 | // readFileSync will crash on non-files. Skip over these 13 | let stat = fs.lstatSync(fs.realpathSync(file)); 14 | if (!stat.isFile()) { 15 | return; 16 | } 17 | if (file.indexOf('increase-memory-limit') >= 0) { 18 | return; 19 | } 20 | // build scripts will hand in LIMIT via cross-env 21 | // avoid updating it while we are running it 22 | if (file.indexOf('cross-env') >= 0) { 23 | return; 24 | } 25 | let contents = fs.readFileSync(file).toString(); 26 | let lines = contents.split('\n') 27 | 28 | let patchedContents = ""; 29 | 30 | for (var index = 0; index < lines.length; index++) { 31 | var line = lines[index]; 32 | if (line.startsWith("if [") || line.startsWith("@IF") || line.indexOf ('has_node') !== -1) { 33 | patchedContents += line + "\n"; 34 | } else { 35 | patchedContents += line.replace(/node(\.exe)?\b(?: \-\-max\-old\-space\-size\=[0-9]+)?/, `node$1 --max-old-space-size=${maxOldSpaceSize}`) + "\n"; 36 | } 37 | } 38 | 39 | fs.writeFileSync(file, patchedContents); 40 | console.log(`'${file.replace(cwd, "")}'`, "written successfully."); 41 | }); 42 | 43 | }); 44 | --------------------------------------------------------------------------------