Nextjs 无法在生产 node_env 的“.next"目录中找到有效的构建 [英] Nextjs fails to find valid build in the '.next' directory in production node_env
问题描述
我在 docker 中运行我的应用程序,但我的生产构建和启动脚本仅在 docker 环境中失败.虽然 node_env 开发在 docker 环境中运行良好.
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.
这是我无法进行生产构建和启动服务器的脚本.我正在使用 nodemon 和 babel
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"
}
}
但是当我在 docker 环境中给出相同的命令时:
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"]
我在 docker 中收到以下错误:
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.
推荐答案
随着时间的推移,这个问题已经获得了相当多的额外投票,让我详细说明一下您在面对上述问题时可能需要考虑的一些事情问题.
As this issue has gotten quite a few extra votes over time, let me elaborate a bit on a few things you might want to take into account when facing the above problem.
版本
请参考官方 docs,其中 Vercel 列出了 Node.js 版本 10.13或更高版本
作为要求.
Please refer to the official docs where Vercel lists Node.js version 10.13 or later
as a requirement.
使用强化图像进行生产
我还建议使用经过生产强化并被证明是安全的图像.使用像 Bitnami (为什么?,示例).请注意,这将解决您的问题,因为现在您不再使用任何本地文件.
I would also recommend using images that are production hardened and proven to be secure. It's considered good practice to use open source images like those of Bitnami (why?, example). Note that this will solve your problem, as now you no longer use any of your local files.
保证构建完整性
建议在您的 .dockerignore
文件中忽略 node_modules
以及任何构建工件(如 .next
)并为 yarn 安装缓存文件夹
或 npm
在构建期间.这样您就可以确保为您的图像生成具有正确操作系统绑定的 node_modules
.
Recommend ignoring node_modules
along with any build artifacts like .next
in your .dockerignore
file and mounting the cache folder for yarn
or npm
during build time. That way you can be sure to generate node_modules
with the correct OS bindings for your image.
这篇关于Nextjs 无法在生产 node_env 的“.next"目录中找到有效的构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!