如何构建 next.js 生产?

新手上路,请多包涵

我尝试在 next.js 中获取生产版本以在我的服务器上运行它,但是当我尝试时我无法构建 next.js 生产版本

npm 运行构建

有谁知道如何让 next.js 中的 prod 构建正常工作我在 next.js 文档中做了所有事情,但总是在下面出现这个错误。如果我进行开发构建,它工作得很好,但尝试产品构建会导致错误。

我也做了很多次 构建 并重新安装了所有仍然有这个错误的 node_modules 包。

它总是在终端显示我

Error: Could not find a valid build in the '/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/.next' directory! Try building your app with 'next build' before starting the server.
    at Server.readBuildId (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/next/dist/server/next-server.js:753:15)
    at new Server (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/next/dist/server/next-server.js:80:25)
    at module.exports (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/next/dist/server/next.js:6:10)
    at Object.<anonymous> (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/next.config.js:6:13)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at loadConfig (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/next/dist/server/config.js:47:28)
    at _callee2$ (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/next/dist/build/index.js:52:42)
    at tryCatch (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/regenerator-runtime/runtime.js:288:22)
    at Generator.prototype.(anonymous function) [as next] (/mnt/c/Users/NZXT_YOLO/Desktop/New folder (2)/learnnextjs-demo/node_modules/regenerator-runtime/runtime.js:114:21)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! hello-next@1.0.0 build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hello-next@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/kk/.npm/_logs/2018-12-10T19_58_00_588Z-debug.log

服务器.js

 const express = require("express");
const next = require("next");

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV === "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.get("*", (req, res) => {
    return handle(req, res);
  });

  server.listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${port}`);
  });
});

next.config.js

 const express = require("express");
const next = require("next");

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV === "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.get("/projects/:page", (req, res) => {
    const page = req.params.page;
    let file = "";
    switch (page) {
      case "example1":
        file = "/projects/example1";
        break;
      case "example2":
        file = "/projects/example2";
        break;
    }
    return app.render(req, res, file, { page });
  });

  server.get("*", (req, res) => {
    return handle(req, res);
  });

  server.listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${port}`);
  });
});

包.json

  {
  "name": "hello-next",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "export": "next export"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "express": "^4.16.4",
    "next": "^7.0.2",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "redux": "^4.0.1",
    "video-react": "^0.13.1"
  }
}

如果有人有想法那就太好了!我计划在我的 AWS 服务器上使用节点运行这个 next.js 站点。但要做到这一点,我需要获得 react.js 的生产版本,目前我只能运行开发版本。

希望有人有想法。

提前致谢!

原文由 Cody 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

似乎您的 server.js 配置不正确。请尝试将您所有的 next.config.js 移动到 server.js 确保 next.config.js 文件为空,然后创建一个新的 npm 运行脚本:

 "prod_start": "NODE_ENV=production node server.js"

您的 package.json 应该如下所示:

 {
  "name": "hello-next",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "prod_start": "NODE_ENV=production node server.js",
    "export": "next export"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "express": "^4.16.4",
    "next": "^7.0.2",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "redux": "^4.0.1",
    "video-react": "^0.13.1"
  }
}

确保运行: npm run build && npm run prod_start

然后你应该有一个使用 next.js 运行的 react 的生产构建

如果您有问题,请告诉我。

原文由 Karim 发布,翻译遵循 CC BY-SA 4.0 许可协议

next build 后面跟着 next start 应该是为生产准备构建并运行它的正确命令。

这是 package.json 的示例。如果你想导出应用程序作为静态内容运行,比如将它作为静态网站托管在 s3 中,你需要运行 next export

 ...
"scripts": {
    "build": "next build",
    "start": "next start",
    "export": "next export"
}
...

确保您的 package.json 中有上述脚本--- 然后按顺序运行以下命令

$ npm run build
$ npm run start

如果要使用特定端口启动应用程序,可以指定 -p 端口作为 npm run 命令的参数

npm run start -- -p 3232

如果你想把它合并到 CI/CD 管道中,你需要有 Dockerfile ,这是一个简单的例子

FROM node:alpine

#copy source
COPY . /app

# Install deps
RUN cd /app &&  npm install

# Build
RUN npm run build

ENTRYPOINT [ "npm", "run", "start" ]

仍需要更多解释或帮助,请随时发表评论,我将非常乐意提供帮助。

原文由 Muhammad Soliman 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题