尝试使用 'pkg' 构建 'nextjs' 应用会导致依赖性错误 and/or 构建后出现 404 错误

Attempting to use 'pkg' to build 'nextjs' app results in dependency errors and/or 404 errors once built

我正在尝试基于示例 here using zeit/pkg 构建小型 NextJS 示例应用程序的可执行文件。问题是唯一可行的入口点(server.js)似乎给了我一堆当前未在与 webpack 编译相关的问题中列出的错误。

如果您只想复制 follow the instructions here,然后添加 pkg。另外 - 这是下面的错误输出。

ERROR Failed to compile with 2 errors
10:48:35 AM These dependencies were not found:

  • @babel/runtime/helpers/extends in D:/snapshot/pkg-ssr/node_modules/next/dist/lib/app.js
  • webpack-hot-middleware/client?autoConnect=false&overlay=false&reload=true in D:/snapshot/pkg-ssr/node_modules/next/dist/client/webpack-hot-middleware-client.js

To install them, you can run: npm install --save @babel/runtime/helpers/extends webpack-hot-middleware/client?autoConnect=false&overlay=false&reload=true (node:15964) DeprecationWarning: Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead

Ready on http://localhost:3000 Client pings, but there's no entry for page: /about { Error: Cannot find module 'D:_experiments\pkg-ssr.next\build-manifest.json' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15) at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1278:46) at Function.Module._load (internal/modules/cjs/loader.js:518:25) at Module.require (internal/modules/cjs/loader.js:648:17) at Module.require (pkg/prelude/bootstrap.js:1157:31) at require (internal/modules/cjs/helpers.js:20:18) at _callee3$ (D:\snapshot\pkg-ssr\node_modules\next\dist\server\render.js:202:29) at tryCatch (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:62:40) at Generator.invoke [as _invoke] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:296:22) at Generator.forEach.prototype.(anonymous function) [as next] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:114:21) at step (D:\snapshot\pkg-ssr\node_modules\@babel\runtime\helpers\asyncToGenerator.js:12:30) at _next (D:\snapshot\pkg-ssr\node_modules\@babel\runtime\helpers\asyncToGenerator.js:27:9) at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' } Client pings, but there's no entry for page: /about { Error: Cannot find module 'D:_experiments\pkg-ssr.next\build-manifest.json' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15) at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1278:46) at Function.Module._load (internal/modules/cjs/loader.js:518:25) at Module.require (internal/modules/cjs/loader.js:648:17) at Module.require (pkg/prelude/bootstrap.js:1157:31) at require (internal/modules/cjs/helpers.js:20:18) at _callee3$ (D:\snapshot\pkg-ssr\node_modules\next\dist\server\render.js:202:29) at tryCatch (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:62:40) at Generator.invoke [as _invoke] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:296:22) at Generator.forEach.prototype.(anonymous function) [as next] (D:\snapshot\pkg-ssr\node_modules\regenerator-runtime\runtime.js:114:21) at step (D:\snapshot\pkg-ssr\node_modules\@babel\runtime\helpers\asyncToGenerator.js:12:30) at _next (D:\snapshot\pkg-ssr\node_modules\@babel\runtime\helpers\asyncToGenerator.js:27:9) at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' }

就我而言,问题是我没有打包构建的应用程序。我需要做一些事情:

  • package.json 中指定 assetsscripts 如下(注意 dist 实际上是 server 在我的例子中,因为我有一个自定义服务器和中间件)

    "pkg": { "assets": [ ".next/**/*" ], "scripts": [ ".next/dist/**/*.js" ] },

  • 首先使用 next build
  • 构建应用程序
  • 然后确保无论我以何种方式构建包,我都使用 package.json。对我来说最好的命令是他们的default examplepkg .

如果你想要一个 repo 和说明来比较,请参阅 Mike Hsu 的以下文章:https://medium.com/@evenchange4/deploy-a-commercial-next-js-application-with-pkg-and-docker-5c73d4af2ee