下一个JS构建在共享主机上失败

当我在共享托管服务器上运行“ npm run build”时,会引发错误:spawn ENOMEM,它在我的本地主机上运行良好,并且在托管服务器上运行良好,直到昨天才运行了几周。

> mysite@1.0.0 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! 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

这是调试日志

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.<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 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 ]

我正在使用express和next js。这是我的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}...` );
    } );
  } );

我已经研究了交换空间/内存,似乎还可以

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

我也尝试过NODE_OPTIONS=--max-old-space-size=2048 npm run build但仍然无法正常工作

有人知道怎么了吗?

2020/03/24 18:28:56

似乎它与NextJs版本9有关,因为它消耗过多的内存来构建应用程序。您可以在这里找到有关该问题的讨论:https : //github.com/zeit/next.js/issues/7929

版本8似乎没有这个问题,因此,如果您选择降级,则可以立即进行。

此外,如果仍然不能解决问题,请在next.config.js中禁用最小化将减少内存使用量:

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

如果您不想排除最小化问题,我发现使用版本8可以在本地构建应用程序并将已构建的文件夹移至共享主机,而不必再次在共享主机上运行“下一个构建”。对于版本9,我无法做到这一点。

更新:在NextJs版本8.1上测试