title: Next JS build failed on shared hosting
body: <p>When I run "npm run build" on my shared hosting server it throws an error: spawn ENOMEM
  It runs fine on my localhost and was running fine on the hosting server for a couple of weeks until yesterday.</p>
  
  <pre><code>&gt; mysite@1.0.0 build /home/user123/public_html
  &gt; next build
  
  Creating an optimized production build ...internal/child_process.js:366
      throw errnoException(err, 'spawn');
      ^
  
  Error: spawn ENOMEM
      at ChildProcess.spawn (internal/child_process.js:366:11)
      at spawn (child_process.js:551:9)
      at Object.fork (child_process.js:113:10)
      at ChildProcessWorker.initialize (/home/user123/public_html/node_modules/jest-worker/build/workers/ChildProcessWorker.js:137:44)
      at new ChildProcessWorker (/home/user123/public_html/node_modules/jest-worker/build/workers/ChildProcessWorker.js:127:10)
      at WorkerPool.createWorker (/home/user123/public_html/node_modules/jest-worker/build/WorkerPool.js:44:12)
      at new BaseWorkerPool (/home/user123/public_html/node_modules/jest-worker/build/base/BaseWorkerPool.js:82:27)
      at new WorkerPool (/home/user123/public_html/node_modules/jest-worker/build/WorkerPool.js:30:1)
      at new JestWorker (/home/user123/public_html/node_modules/jest-worker/build/index.js:131:26)
      at TaskRunner.run (/home/user123/public_html/node_modules/next/dist/build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js:3:166)
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! mysite@1.0.0 build: `next build`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the mysite@1.0.0 build 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!     /home/user123/.npm/_logs/2019-09-24T19_27_41_300Z-debug.log
  </code></pre>
  
  <p>and this is the debug log </p>
  
  <pre><code>0 info it worked if it ends with ok
  1 verbose cli [ '/home/user123/node/bin/node',
  1 verbose cli   '/home/user123/node/bin/npm',
  1 verbose cli   'run',
  1 verbose cli   'build' ]
  2 info using npm@6.11.3
  3 info using node@v10.16.3
  4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
  5 info lifecycle mysite@1.0.0~prebuild: mysite@1.0.0
  6 info lifecycle mysite@1.0.0~build: mysite@1.0.0
  7 verbose lifecycle mysite@1.0.0~build: unsafe-perm in lifecycle true
  8 verbose lifecycle mysite@1.0.0~build: PATH: /home/user123/node/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/user123/public_html/node_modules/.bin:/home/user123/node/bin:/home/user123/node/bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/jdk/bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/cpanel/composer/bin:/opt/puppetlabs/bin:/opt/dell/srvadmin/bin:/usr/local/bin:/usr/X11R6/bin:/home/user123/.local/bin:/home/user123/bin
  9 verbose lifecycle mysite@1.0.0~build: CWD: /home/user123/public_html
  10 silly lifecycle mysite@1.0.0~build: Args: [ '-c', 'next build' ]
  11 silly lifecycle mysite@1.0.0~build: Returned: code: 1  signal: null
  12 info lifecycle mysite@1.0.0~build: Failed to exec build script
  13 verbose stack Error: mysite@1.0.0 build: `next build`
  13 verbose stack Exit status 1
  13 verbose stack     at EventEmitter.&lt;anonymous&gt; (/home/user123/node/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
  13 verbose stack     at EventEmitter.emit (events.js:198:13)
  13 verbose stack     at ChildProcess.&lt;anonymous&gt; (/home/user123/node/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
  13 verbose stack     at ChildProcess.emit (events.js:198:13)
  13 verbose stack     at maybeClose (internal/child_process.js:982:16)
  13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
  14 verbose pkgid mysite@1.0.0
  15 verbose cwd /home/user123/public_html
  16 verbose Linux 2.6.32-954.3.5.lve1.4.66.el6.x86_64
  17 verbose argv "/home/user123/node/bin/node" "/home/user123/node/bin/npm" "run" "build"
  18 verbose node v10.16.3
  19 verbose npm  v6.11.3
  20 error code ELIFECYCLE
  21 error errno 1
  22 error mysite@1.0.0 build: `next build`
  22 error Exit status 1
  23 error Failed at the mysite@1.0.0 build script.
  23 error This is probably not a problem with npm. There is likely additional logging output above.
  24 verbose exit [ 1, true ]
  </code></pre>
  
  <p>I am using express and next js .
  This is my server.js</p>
  
  <pre><code>const express = require( 'express' );
  const next    = require( 'next' );
  
  // Import middleware.
  const routes = require( './routes' );
  
  // Setup app.
  const app     = next( { dev: 'production' !== process.env.NODE_ENV } );
  const handle  = app.getRequestHandler();
  const handler = routes.getRequestHandler( app );
  
  app.prepare()
    .then( () =&gt; {
  
      // Create server.
      const server = express();
  
      // Use our handler for requests.
      server.use( handler );
  
      // Don't remove. Important for the server to work. Default route.
      server.get( '*', ( req, res ) =&gt; {
        return handle( req, res );
      } );
  
      // Get current port.
      const port = process.env.PORT || 8080;
  
      // Error check.
      server.listen( port, err =&gt; {
        if ( err ) {
          throw err;
        }
  
        // Where we starting, yo!
        console.log( `&gt; Ready on port ${port}...` );
      } );
    } );
  </code></pre>
  
  <p>I've looked into swap space / memory and it seems like it's fine</p>
  
  <pre><code>             total       used       free     shared    buffers     cached
  Mem:         31906      31330        575         21       2982      16900
  -/+ buffers/cache:      11447      20459
  Swap:         8191          0       8191
  </code></pre>
  
  <p>I've also tried doing <code>NODE_OPTIONS=--max-old-space-size=2048 npm run build</code> and still doesn't work</p>
  
  <p>Anyone have any idea what's wrong?</p>