【问题标题】:Could not find a production build in the '/opt/app/.next' directory在“/opt/app/.next”目录中找不到生产版本
【发布时间】:2021-12-24 14:18:11
【问题描述】:

您使用的是哪个版本的 Next.js?

10.0.5

您使用的是什么版本的 Node.js?

14 高山

你用的是什么浏览器?

您使用的是什么操作系统?

窗户

您如何部署您的应用程序?

Dockerfile 中的下一个构建

描述错误

我的下一次构建和下一次开始工作正常。突然没有任何改变我在运行时收到这个错误

在“/opt/app/.next”目录中找不到生产版本

这是我的 docker 文件

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

这是我的 package.json 脚本

"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",

这是我的 start.js 文件

#!/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))

运行 docker image 后出现此错误

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)

.

预期行为

应用程序应该从http://127.0.0.1:3000开始

复制

如上所述使用 Dockerfile 和 start.js 文件

【问题讨论】:

    标签: reactjs docker next.js


    【解决方案1】:

    当您从该图像创建容器时,您的 .next 目录为空。

    相应地更新 docker 文件。

    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
    

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 2018-09-15
      • 2018-11-29
      • 1970-01-01
      • 2019-01-30
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-07-03
      相关资源
      最近更新 更多