NextJS在“ .next”目录中找不到有效的内部版本

在问这个问题之前,我看了以下问题,但是我相信我是不同的,因为我没有使用Docker:Nextjs无法在生产node_env的'.next'目录中找到有效的版本。

我也试过这个去掉“接下来”文件夹,但是仍然得到同样的问题的方法。

解决了许多其他问题之后,我只能解决一个似乎无法解决的问题。当我尝试部署到Heroku时,不断出现以下错误:

node server.js

Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.

这是我的package.json文件:

{
  "name": "StarterApp",
  "version": "1.0.0",
  "engines": {
    "node": "10.4.1"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha",
    "dev": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "4.16.3",
    "fs-extra": "^5.0.0",
    "ganache-cli": "^6.1.3",
    "mocha": "^5.2.0",
    "next": "^4.2.3",
    "next-routes": "^1.4.2",
    "node-gyp": "^3.7.0",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "rebuild": "^0.1.2",
    "semantic-ui-css": "^2.3.2",
    "semantic-ui-react": "^0.79.1",
    "sha3": "^1.2.2",
    "solc": "^0.4.24",
    "truffle-hdwallet-provider": "0.0.3",
    "web3": "^1.0.0-beta.34"
  }
}

Server.js文件:

const { createServer } = require('http');
const next = require('next');

const app = next({
  dev: process.env.NODE_ENV !== 'production'
});

const routes = require('./routes');
const handler = routes.getRequestHandler(app);

app.prepare().then(() => {
  createServer(handler).listen(5000, (err) => {
    if (err) throw err;
    console.log('Ready on localhost:5000');
  });
});

该应用程序在本地部署没有问题,但是在部署到Heroku时出现此错误。我究竟做错了什么?

番长猴子2020/03/23 14:39:31
npm run build

然后

npm run start

解决了我的问题。

DavaidTony宝儿2020/03/23 14:39:31

NextJS的构建可能很大(取决于您的项目大小),这可能会非常庞大​​,在部署过程中可能会花费您大量资金。您可以将以下内容应用于package.json

{
    "script": {
        "build": "next build",
        "heroku-postbuild": "npm run build",
        "start": "next start"
    }
}