如何构建 next.js 产品?

Cod*_*ody 8 node.js reactjs next.js

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

npm 运行构建

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

我也做了很多次next build并重新安装了所有仍然存在此错误的 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
Run Code Online (Sandbox Code Playgroud)

服务器.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}`);
  });
});
Run Code Online (Sandbox Code Playgroud)

下一个.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}`);
  });
});
Run Code Online (Sandbox Code Playgroud)

包.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

希望有人有想法。

提前致谢!

Muh*_*man 7

next build其次next start应该是正确的命令来为生产准备构建并运行它。

下面是一个例子package.json。如果您想导出应用程序以作为静态内容运行,例如将其作为静态网站托管在 s3 中,您需要运行next export

...
"scripts": {
    "build": "next build",
    "start": "next start",
    "export": "next export"
}
...
Run Code Online (Sandbox Code Playgroud)

确保您有上述脚本,package.json然后按顺序运行以下脚本

$ npm run build 
$ npm run start
Run Code Online (Sandbox Code Playgroud)

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

npm run start -- -p 3232
Run Code Online (Sandbox Code Playgroud)

如果你想把它合并到 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" ]
Run Code Online (Sandbox Code Playgroud)

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


小智 5

看来你的server.js配置不正确。请尝试移动您拥有的所有内容next.config.js以server.js确保next.config.js文件为空,然后创建一个新的 npm 运行脚本:

"prod_start": "NODE_ENV=production node server.js"
Run Code Online (Sandbox Code Playgroud)

你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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • @Karim,我正在使用类似的设置(express + next.js),您能告诉我如何构建和部署 next.js 应用程序吗?当运行下一个构建命令时,它会生成 .next 文件夹。我不知道是否需要将所有应用程序源(server.js 等)移动到生产服务器并运行 npm start,因为我的express.js 服务器文件没有移动/捆绑到 .next 文件夹。 (2认同)