首页 > 解决方案 > 如何设置为heroku nextjs

问题描述

我在 package.json 中使用此代码,我想部署到 heroku 应用程序

"scripts": {
    "build": "cd app && next build",
    "start": "npm run build && env NODE_ENV=production node app.js",
    "dev": "nodemon --ignore app/ app.js"   
},

我也试过:

"start": "next start -p $PORT",

我正在使用这个样板: https ://github.com/MustansirZia/next-express-bootstrap-boilerplate

标签: next.js

解决方案


我相信它在错误的端口上运行。Heroku 会将端口作为环境变量提供给您,但您的应用需要从环境中获取端口,然后在启动时使用它。

在 app.js 中将其添加到顶部(将默认端口更改为您想要的任何内容)。它将从环境变量 PORT 中获取端口,如果不存在则默认为 3000:

const port = process.env.PORT || 3000;

然后在最底部,将最后一行更改为此

start(port);

推荐阅读