Leo
Leo

Reputation: 61

Next JS build failed on shared hosting

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.

> [email protected] build /home/user123/public_html
> 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! [email protected] build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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

and this is the debug log

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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle [email protected]~prebuild: [email protected]
6 info lifecycle [email protected]~build: [email protected]
7 verbose lifecycle [email protected]~build: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~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 [email protected]~build: CWD: /home/user123/public_html
10 silly lifecycle [email protected]~build: Args: [ '-c', 'next build' ]
11 silly lifecycle [email protected]~build: Returned: code: 1  signal: null
12 info lifecycle [email protected]~build: Failed to exec build script
13 verbose stack Error: [email protected] build: `next build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/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.<anonymous> (/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 [email protected]
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 [email protected] build: `next build`
22 error Exit status 1
23 error Failed at the [email protected] build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I am using express and next js . This is my server.js

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( () => {

    // 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 ) => {
      return handle( req, res );
    } );

    // Get current port.
    const port = process.env.PORT || 8080;

    // Error check.
    server.listen( port, err => {
      if ( err ) {
        throw err;
      }

      // Where we starting, yo!
      console.log( `> Ready on port ${port}...` );
    } );
  } );

I've looked into swap space / memory and it seems like it's fine

             total       used       free     shared    buffers     cached
Mem:         31906      31330        575         21       2982      16900
-/+ buffers/cache:      11447      20459
Swap:         8191          0       8191

I've also tried doing NODE_OPTIONS=--max-old-space-size=2048 npm run build and still doesn't work

Anyone have any idea what's wrong?

Upvotes: 6

Views: 3694

Answers (1)

hstjep
hstjep

Reputation: 33

It seems like it is related to the NextJs version 9 as it consumes too much memory for building the app. You can find discussion about that issue here: https://github.com/zeit/next.js/issues/7929

Version 8 seems to not have that issue, so if downgrading is your option, you can do that for now.

Additionally, if that still doesn't help, disabling minimization in next.config.js will reduce memory usage:

module.exports = {
    webpack: (config, options) => {
        config.optimization.minimize = false;
      return config
    }
  }

If you do not want to exclude minimization, I found out that with version 8 you can build your app locally and move already built folder to your shared hosting without having to run "next build" on shared hosting again. I couldn't do that with version 9.

Update: Tested on NextJs version 8.1

Upvotes: 2

Related Questions