首页 > 解决方案 > NextJS 在 Firebase Functions Deploy 上引发错误

问题描述

TLDR;我正在关注 NextJS 中使用 Firebase 的示例,并且通过最小的更改我无法推送到 Firebase。

我正在关注 NextJS with-firebase-hosting-and-typescript示例,并根据#8893的帮助。

我将deploy脚本更改package.jsoncross-env NODE_ENV=production firebase deploy.

我还将conf值更改functions/index.ts

conf: {
    distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next`
}

当我将应用程序部署到 firebase 时,我现在收到一个错误

部署错误。为您的函数设置执行环境时出错。请在几分钟后再次尝试部署。

我做了一些调试,如果我注释掉这一行

const app = next({ dev, conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` } 
})

在 中functions/index.ts,那么这些功能将部署得很好。所以,问题似乎与next()

这是 的代码functions/index.ts,这会引发错误。

import * as functions from 'firebase-functions'
import next from 'next'
import * as path from 'path'



const appSetup = { 
  dev: process.env.NODE_ENV !== 'production', 
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` } 
}
console.log("appSetup: ", appSetup)
const app = next(appSetup)
// const handle = app.getRequestHandler()

export const nextApp = functions.https.onRequest(async(req, res) => {
  // return app.prepare().then(() => handle(req, res))
  return res.send({ status: "Hello from Firebase!, nextApp" })
})

这是的代码functions/index.ts,这不会引发错误

import * as functions from 'firebase-functions'
import next from 'next'
import * as path from 'path'



const appSetup = { 
  dev: process.env.NODE_ENV !== 'production', 
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` } 
}
console.log("appSetup: ", appSetup)
// const app = next(appSetup)
// const handle = app.getRequestHandler()

export const nextApp = functions.https.onRequest(async(req, res) => {
  // return app.prepare().then(() => handle(req, res))
  return res.send({ status: "Hello from Firebase!, nextApp" })
})

package.json

"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0",
"next": "^9.3.5",
"react": "16.13.1",
"react-dom": "16.13.1"

标签: reactjstypescriptfirebasegoogle-cloud-functionsnext.js

解决方案


对于任何在同一问题上苦苦挣扎的人。解决方法是functions/index.ts

我需要更换

conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` }

conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }

推荐阅读