Nextjs fails to find valid build in the '.next' directory in production node_env

怎甘沉沦 提交于 2019-12-22 04:00:31

问题


I am running my app in docker, but my production build and start script fails only in docker environment. Although node_env development works well in docker environment.

Here is my script that fails to make a production build and start a server. I am using nodemon and babel

"build:prod": {
      "command": "babel ./src/server/ -d server --presets es2015,stage-2 && next build src",
      "env": {
        "NODE_ENV": "production"
      }
    },
    "start:prod": {
      "command": "PORT=3000 nodemon --watch ./src/server/ ./src/server/server.js --exec babel-node --presets es2015,stage-2",
      "env": {
        "NODE_ENV": "production"
      }
    }

But when I give same command in docker environment:

FROM node:8-alpine

COPY package.json /tmp/package.json

RUN cd /tmp && npm install

RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app

WORKDIR /opt/app

ADD . /opt/app

RUN npm run build:prod

EXPOSE 3000

CMD ["npm", "run", "start:prod"]

I get the following error in docker:

 > better-npm-run start:prod
site_1      | 
site_1      | running better-npm-run in /opt/app
site_1      | Executing script: start:prod
site_1      | 
site_1      | to be executed: PORT=3000 NODE_ENV=production nodemon --watch ./src/server/ ./src/server/server.js --exec babel-node --presets es2015,stage-2 
site_1      | [nodemon] 1.17.3
site_1      | [nodemon] to restart at any time, enter `rs`
site_1      | [nodemon] watching: /opt/app/src/server/**/*
site_1      | [nodemon] starting `babel-node ./src/server/server.js --presets es2015,stage-2`
site_1      | false 'production'
site_1      | > Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.
site_1      | [nodemon] app crashed - waiting for file changes before starting...

I would appreciate any help and would be nice to know what I am doing wrong.


回答1:


You need to make sure the .next directory is not being copied from your host

ADD . /opt/app

Will also add the .next directory you had on host. I would add .dockerignore and add .next to the same. And then build and run again



来源:https://stackoverflow.com/questions/49676338/nextjs-fails-to-find-valid-build-in-the-next-directory-in-production-node-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!