如何构建next.js产品?

iqih9akk  于 2023-06-22  发布在  其他
关注(0)|答案(4)|浏览(132)

我尝试在next.js中获取生产构建以在我的服务器上运行它,但当我尝试时,我无法构建next.js生产构建
npm run build
有谁知道如何在next.js中正确构建prod吗?我做了next.js文档中的所有事情,但总是得到下面的错误。如果我做一个开发构建,它工作得很好,但尝试prod构建会导致错误。
我也做了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

server.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}`);
  });
});

package.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服务器上使用node运行这个next.js站点。但要做到这一点,我需要获得react.js的生产版本,目前我只能运行一个开发版本。

ubby3x7f

ubby3x7f1#

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

仍然需要更多的解释或帮助,不要犹豫,留下评论,我将非常乐意提供帮助。

gdx19jrr

gdx19jrr2#

看来你的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的生产构建
如果你有问题就告诉我。

inn6fuwd

inn6fuwd3#

您必须在根文件夹中启动next build,而不是在.next/中启动

sqserrrh

sqserrrh4#

有三种方法:
方法1:使用next build代替npm run build
方式2:npm run buildnpm install -g serveserve -s build更多信息:https://create-react-app.dev/docs/deployment/
方法3:在npm run build之后,从JS中删除/,从/static/index.html文件中删除CSS链接。例如,替换这两行

<script defer="defer" src="/static/js/main.aa87bc08.js"></script>
<link href="/static/css/main.073c9b0a.css" rel="stylesheet"/>

用这两条线

<script defer="defer" src="static/js/main.aa87bc08.js"></script>
<link href="static/css/main.073c9b0a.css" rel="stylesheet" />

现在它甚至可以在file:///D:/codes/ProjectName/build/index.html上工作
在评论中告诉我,如果3种方法都不起作用,我会找到,尝试并告诉方式4,5等。

相关问题