Amit Shakya
Amit Shakya

Reputation: 994

Unable to run NextJS based node server on AWS EC2 instance

I have tried to run my node server multiple times I am getting this error, for this I tried to delete .next folder and build again after build complete it throws same error. It is running perfectly on my local machine.

`Could not find a valid build in the '${this.distDir}' directory! Try building your app with 'next build' before starting the server

Error: Could not find a valid build in the '/root/only4laughproject/.next' directory! Try building your app with 'next build' before starting the server.
at Server.readBuildId (/root/only4laughproject/node_modules/next-server/dist/server/next-server.js:435:23)
at new Server (/root/only4laughproject/node_modules/next-server/dist/server/next-server.js:44:29)
at module.exports (/root/only4laughproject/node_modules/next-server/index.js:4:10)
at module.exports (/root/only4laughproject/node_modules/next/dist/server/next.js:2:161)
at Object.<anonymous> (/root/only4laughproject/server.js:9:13)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)

here is my package.json script

    "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "npm run-script build && cross-env NODE_ENV=production node server.js",
    "go": "cross-env NODE_ENV=production node server.js"
  }

Upvotes: 0

Views: 842

Answers (1)

Amit Shakya
Amit Shakya

Reputation: 994

on doing npm start command it is running next build command before project build finish it starts my server.js that's why this problem occur, I tried next build after build complete I hit node server.js. It resolve my issue.

Upvotes: 1

Related Questions