Answer a question

What version of Next.js are you using?

10.0.5

What version of Node.js are you using?

14 alpine

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

next build in Dockerfile

Describe the Bug

My next build & next start was working fine. Suddenly without any change i am getting this error during runtime

Could not find a production build in the '/opt/app/.next' directory

Here is my docker file

FROM node:14-alpine

WORKDIR /opt/app
RUN chown -R node:node /opt/app

USER node

ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

COPY --chown=node:node package*.json ./

RUN npm ci

COPY --chown=node:node . /opt/app

RUN npm install --dev && npm run lint && npm run build:app

RUN ls -la

CMD [ "npm", "start" ]

EXPOSE 3000

here is my package.json scripts

"start": "cross-env NODE_ENV=production node ./bin/start.js",
"build:app": "cross-env NODE_ENV=production rm -rf .next && APP_TENANT_CODE=app && next build",

here is my start.js file

#!/usr/bin/env node
/**
 * So next.config.js doesn't allow async functions, but we need to read
 * runtime config variables from Key Vault.
 *
 * So instead we will read them and then start next.js server in  custom script.
 *
 */
const path = require('path')
const { default: startServer } = require('next/dist/server/lib/start-server')

process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))

async function main() {
  const port = process.env.PORT || 3000
  const dir = path.resolve(__dirname, '..')
  const hostname = '0.0.0.0'

  // start the app
  const app = await startServer({ dir }, port, hostname)
  console.log(`started server on http://${hostname}:${port}`)
  await app.prepare()
}

main().catch(error => console.error(error))

after running docker image i am getting this error

Error: Could not find a production build in the '/opt/app/.next' directory. Try building your app with 'next build' before starting the production server. https://err.sh/vercel/next.js/production-start-no-build-id
    at Server.readBuildId (/opt/app/node_modules/next/dist/next-server/server/next-server.js:146:355)
    at new Server (/opt/app/node_modules/next/dist/next-server/server/next-server.js:3:120)
    at createServer (/opt/app/node_modules/next/dist/server/next.js:2:638)
    at start (/opt/app/node_modules/next/dist/server/lib/start-server.js:1:323)
    at main (/opt/app/bin/start.js:52:21)
    at Object.<anonymous> (/opt/app/bin/start.js:57:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)

.

Expected Behavior

application should start on http://127.0.0.1:3000

To Reproduce

use Dockerfile and start.js file as mentioned above

Answers

Your .next directory is empty when you create the container from that image.

update the docker file accordingly.

FROM node:16
WORKDIR /opt/app

RUN chown -R node:node /opt/app
USER node

ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

COPY --chown=node:node package*.json ./
RUN npm install

COPY --chown=node:node . /opt/app
RUN npm run lint

RUN npm run build:fair
CMD [ "npm", "run", "start" ]

EXPOSE 3000
Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐