编辑:
我想将 Nuxt3 应用部署到 Firebase 托管。我已将 Nitro 的部署预设设置为“firebase”
NITRO_PRESET=firebase
,并且构建步骤运行良好。但是,当我运行 firebase deploy
时,我收到一条错误消息:
There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
我检查了
.output/server
目录,看到Nitro生成了一个名为index.mjs
的文件。我检查了 Cloud Functions 是否也适用于 .mjs
文件,但情况似乎并非如此。我寻找可能的解决方案,但什么也没找到;我尝试重新安装模块,但没有成功。我是否有什么配置错误或损坏的地方?
我正在遵循本教程: https://nitro.unjs.io/deploy/providers/firebase
我在 Windows 11 上使用 WSL2 Ubuntu,项目文件夹位于 Linux 文件系统内。
这是我的
package.json
:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/google-fonts": "^3.0.0-0",
"@types/three": "^0.143.1",
"nuxt": "3.0.0-rc.8"
},
"dependencies": {
"firebase": "^9.9.3",
"sass": "^1.54.5",
"three": "^0.143.0",
"vuex": "^4.0.2",
"sass-loader": "^13.0.2"
}
}
我的 firebase.json:
{
"functions": {
"source": "./.output/server",
"runtime": "nodejs16",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
},
"hosting": {
"site": "my-site",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
}
构建步骤之后:
Σ Total size: 19.6 MB (3.21 MB gzip)
✔ You can deploy this build using npx firebase deploy
部署步骤后:
i deploying functions, hosting
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
i functions: preparing codebase default for deployment
Error: There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
您似乎正在使用默认的
node-server
预设。要将 Nuxt 3 应用部署到 Firebase,您必须在您的环境中将 NITRO_PRESET
设置为 firebase
。
NITRO_PRESET=firebase
npm run build
构建完成后,CLI 应提示您运行
npx firebase deploy
。
然后运行命令,它将把应用程序部署到 Firebase 托管和功能。
您可以在文档中阅读更多相关信息。
我想添加命令行环境变量的替代方案。您还可以在您的
nuxt.config.ts
文件中添加此函数。
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
...
nitro: {
preset: 'firebase',
firebase: {
gen: 2,
httpsOptions: {
region: '<REGIOIN, for instance us-west1>',
maxInstances: 3,
},
},
},
});
这是我的首选选项,因此它可以通用,并且您不需要事先设置 env 变量。
设置完成后,它与正常的构建和部署相同。
firebase deploy