How to increase NodeJS heap (--max-old-space-size) in Bitbucket Pipelines for Typescript/Webpack?

故事扮演 提交于 2019-12-12 20:45:49

问题


I am running into memory issues during the webpack packaging step in a serverless/TypeScript project.

I have tried:

  • 'increase-memory-limit' npm package to no avail
  • Calling the following command from an npm run script

  • Calling the following command directly through bitbucket-pipelines.yml.

    node --max-old-space-size=4096 ./node_modules/.bin/serverless deploy
    

It works fine locally with this command, but in Bitbucket pipelines I get the following output:

    Serverless: Bundling with Webpack...

    internal/child_process.js:323
        throw errnoException(err, 'spawn');
        ^
    Error: spawn ENOMEM
        at _errnoException (util.js:1022:11)
        at ChildProcess.spawn (internal/child_process.js:323:11)
        at exports.spawn (child_process.js:502:9)
        at Object.exports.fork (child_process.js:103:10)
        at fork (/opt/atlassian/pipelines/agent/build/node_modules/worker-farm/lib/fork.js:17:36)
        at Farm.startChild (/opt/atlassian/pipelines/agent/build/node_modules/worker-farm/lib/farm.js:106:16)
        at Farm.processQueue (/opt/atlassian/pipelines/agent/build/node_modules/worker-farm/lib/farm.js:279:10)
        at Farm.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/worker-farm/lib/farm.js:97:21)
        at ontimeout (timers.js:475:11)
        at tryOnTimeout (timers.js:310:5)
        at Timer.listOnTimeout (timers.js:270:5)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! platform-state-machine-import@0.0.2 deploy: `node --max-old-space-size=4096 ./node_modules/.bin/serverless deploy "--stage"    "feattsify" "--region" "us-east-1"`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the platform-state-machine-import@0.0.2 deploy script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /root/.npm/_logs/2019-02-07T22_37_25_150Z-debug.log

I've done a lot of googling and searching here to try to find if Bitbucket has some kind of env. variable you can set for runtime args, etc., but can't seem to find anything that doesn't tie back into the aforementioned NPM package.


回答1:


I encountered a similar issue some time ago in some Jenkins build tasks. You might be able to solve it using the NODE_OPTION environment variable:

NODE_OPTIONS=--max_old_space_size=4096

From your error stacktrace, it looks like new node processes are beeing spawned. If you can set this environment variable, it will be used by node to pass arguments to any new processes, and so to the spawned processes that run out of memory here.

https://nodejs.org/dist/latest-v8.x/docs/api/cli.html#cli_node_options_options




回答2:


Increase it from your start command e.g

 node --max-old-space-size=8192 server.js  



回答3:


There ended up being two workarounds for this issue. The ENOMEM error was appearing first because I needed more memory for the Node process, and second because the memory I was allotting to the Node process exceeded that which was available in the Bitbucket environment (default 4gb).

Option 1:

Using any of the other answers' options AND ensuring the Bitbucket pipeline environment has enough memory - for me, that meant cranking the size to 2x (see this link).

Option 2:

Adding the 'transpileOnly' flag to your webpack.config.js file, using ts-loader as your transpiler. This skips type-checking and saves memory at package/deploy time. WARNING: You still should type-check your project on build. For me, this meant running tsc before packaging/deploying.

rules: [
  // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
  { 
    test: /\.tsx?$/, 
    loader: 'ts-loader',
    options: {
      transpileOnly: true
    }
  },
],


来源:https://stackoverflow.com/questions/54594610/how-to-increase-nodejs-heap-max-old-space-size-in-bitbucket-pipelines-for-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!