NextJS cannot find a valid build in the '.next' directory

Captain Kirk picture Captain Kirk · Jun 20, 2018 · Viewed 23.2k times · Source

I looked at the following question before asking this one but I believe mine is different because I am not using Docker: Nextjs fails to find valid build in the '.next' directory in production node_env

I also tried this approach of removing the '.next' folder but still get the same issue.

After fixing a host of other issues, I am down to one I cannot seem to resolve. When I try to deploy to Heroku I keep getting the following error:

node server.js

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

Here is my package.json file:

{
  "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 file:

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');
  });
});

The app deploys without issue locally but I get this error when deploying to Heroku. What am I doing wrong?

Answer

Mahendra Pratap picture Mahendra Pratap · Feb 27, 2020
npm run build

then

npm run start

solved my problem.