【发布时间】:2021-10-13 03:15:09
【问题描述】:
我能够按照this post 将我的 Next.js 项目成功部署到 Firebase Functions。但是,当我尝试在浏览器中访问我的项目时,我收到了 Error: could not handle the request 错误,并且在函数日志中我看到了以下错误消息:
Error: Could not find a production build in the '/workspace/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
at Server.readBuildId (/workspace/node_modules/next/dist/next-server/server/next-server.js:151:355)
at new Server (/workspace/node_modules/next/dist/next-server/server/next-server.js:3:120)
at NextServer.createServer (/workspace/node_modules/next/dist/server/next.js:1:2935)
at process._tickCallback (internal/process/next_tick.js:68:7)
我不确定如何解决这个问题...在我的配置文件下面添加:
package.json
{
"main": "server.js", // my next.js app init file
"scripts": {
"dev": "FIREBASE_AUTH_EMULATOR_HOST=localhost:9099 next dev",
"start": "next start",
"build": "next build",
"build:functions": "tsc",
"lint": "npm run lint:next && npm run lint:functions",
"lint:functions": "eslint --ext .js",
"lint:next": "next lint",
"deploy": "firebase deploy --only database,hosting,functions:nextServer",
"emulate": "firebase emulators:start --import=./emulator-data --export-on-exit",
"test": "jest --runInBand --detectOpenHandles",
"cypress": "cypress open"
},
"dependencies": {
// including next
}
}
firebase.json
{
"functions": {
"source": ".",
"runtime": "nodejs10",
"ignore": [
"**/.next/cache/**",
"**/__tests__/**",
"**/components/**",
"**/cypress/**",
"**/emulator-data/**",
"**/functions/**",
"**/layouts/**",
"**/node_modules/**",
"**/pages/**",
"**/public/**",
"**/utils/**",
"**/.*",
"**/*.log",
"**/cypress.json",
"**/database.rules.json",
"**/firebase.json"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build:functions"
]
},
"hosting": {
"target": "my_app",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [{
"source": "**",
"function": "nextServer"
}]
}
}
我假设一旦部署,Firebase Functions 环境将运行 npm run build 和 npm run start 脚本。还是工作流程不同?也许我的脚本不正确?
【问题讨论】:
-
您不应该在
predeploy中调用npm --prefix \"$RESOURCE_DIR\" run build来构建应用程序吗? -
感谢您的建议。我尝试将其更改为
run build,唉,同样的结果......那么,predeploy脚本是在部署后在云功能服务器上运行的脚本吗?如果是这样,命名会很混乱,最好在部署后命名或类似的东西...... -
为了完整起见,您应该将当前形式的
server.js添加到此问题中,而不是依赖查看 previous question 的人。 -
我实际上已将我的问题缩小到
firebase.json的ignore部分。问题是当我包含**/pages/**时,它不仅忽略了顶级页面文件夹,还忽略了.next/server/pages,使我的部署不完整。由于某种原因,仅忽略/pages/**也不起作用...
标签: firebase google-cloud-functions next.js