Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 825
Captain Kirk Avatar asked Jun 20 '18 11:06

Captain Kirk


3 Answers

npm run build

then

npm run start

solved my problem.

like image 133
Mahendra Pratap Avatar answered Sep 19 '22 12:09

Mahendra Pratap


First

npm run-script build

Then

npm run start
like image 43
Azizjon Nigmatov Avatar answered Sep 20 '22 12:09

Azizjon Nigmatov


Just see the error carefully:

Error: Could not find a production build in the 'E:\Developer's Area\weatherteller\.next' directory. Try building your app with 'next build' before s    at Server.readBuildId (E:\Developer's Area\weatherteller\node_modules\next\dist\next-server\server\next-server.js:146:355)
    at new Server (E:\Developer's Area\weatherteller\node_modules\next\dist\next-server\server\next-server.js:3:120)
    at createServer (E:\Developer's Area\weatherteller\node_modules\next\dist\server\next.js:2:638)
    at start (E:\Developer's Area\weatherteller\node_modules\next\dist\server\lib\start-server.js:1:323)
    at nextStart (E:\Developer's Area\weatherteller\node_modules\next\dist\cli\next-start.js:19:125)
    at E:\Developer's Area\weatherteller\node_modules\next\dist\bin\next:27:115

while running

npm start 

It's not able to locate the production build which is required to launch the next app. While creating next app using

npm install next react react-dom --save

.next folder was not created so you need to create the .next folder first using

npm build

which will consist of all your production build files.

After npm build the folder will be created and you can run your app using

npm start

Also, make sure these scripts are in your next app

"scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },

Hope this resolves your error 😀😀

like image 28
Badal S Avatar answered Sep 22 '22 12:09

Badal S