Next JS 在共享主机上构建失败

Posted

技术标签:

【中文标题】Next JS 在共享主机上构建失败【英文标题】:Next JS build failed on shared hosting 【发布时间】:2020-01-24 23:36:30 【问题描述】:

当我在共享主机服务器上运行“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 还是不行

有人知道怎么回事吗?

【问题讨论】:

如果我没记错的话,你只有 32 Mb 的可用内存...... 我遇到了同样的问题,但在仅使用 Webpack 和 Jest 而不是 Next.js 的仅前端 React 应用程序上。 【参考方案1】:

它似乎与 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 版上测试

【讨论】:

以上是关于Next JS 在共享主机上构建失败的主要内容,如果未能解决你的问题,请参考以下文章

WIN7,共享打印机。提示“WINDOWS无法连接到打印机。操作失败,错误为0x000006b5

该主机当前已禁用共享文件夹,请查阅VMware文档了解如何启用

Angular 2. 在共享主机上运行

通过nodejs在共享cpanel主机上实现websocket服务器

Mongoose 没有与 Namecheap 共享主机节点 js 应用上的数据库建立连接

从一台主机到另一台主机的共享库路径不一致