title: How to build next.js production?
body: <p>I try to get a production build in next.js to run it on my server but I can't build next.js production build when I try </p>
  
  <blockquote>
    <p>npm run build </p>
  </blockquote>
  
  <p>Does anyone know how to get a prod build in next.js working correctly I did everything in the next.js documentation but always get this error below. If I do a dev build it works just fine but trying prod build results in errors.</p>
  
  <p>I did also <strong>next build</strong> many times and reinstalled all node_modules packages still having this error.</p>
  
  <p><strong>it always shows me in terminal</strong>    </p>
  
  <pre><code>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.&lt;anonymous&gt; (/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
  </code></pre>
  
  <p>server.js</p>
  
  <pre><code>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(() =&gt; {
    const server = express();
  
    server.get("*", (req, res) =&gt; {
      return handle(req, res);
    });
  
    server.listen(port, err =&gt; {
      if (err) throw err;
      console.log(`&gt; Ready on http://localhost:${port}`);
    });
  });
  </code></pre>
  
  <p>next.config.js</p>
  
  <pre><code>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(() =&gt; {
    const server = express();
  
    server.get("/projects/:page", (req, res) =&gt; {
      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) =&gt; {
      return handle(req, res);
    });
  
    server.listen(port, err =&gt; {
      if (err) throw err;
      console.log(`&gt; Ready on http://localhost:${port}`);
    });
  });
  </code></pre>
  
  <p>package.json</p>
  
  <pre><code> {
    "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"
    }
  }
  </code></pre>
  
  <p>If anyone has an idea would be so nice! I plan to run this next.js site using node on my AWS server. But to do this I need to get production build of react.js currently I can run just a development build.</p>
  
  <p>Hope someone has an idea.</p>
  
  <p>Thanks in advance!</p>