[英]Failed to parse private key: Error: Invalid PEM formatted message
I am trying to deploy a NEXT.JS app that uses firebase-admin on Vercel.我正在尝试部署一个在 Vercel 上使用 firebase-admin 的 NEXT.JS 应用程序。
import * as firebaseAdmin from 'firebase-admin';
import firebase from 'firebase/app';
if (!firebaseAdmin.apps.length) {
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert({
privateKey: process.env.NEXT_PUBLIC_FIREBASE_PRIVATE_KEY,
clientEmail: process.env.NEXT_PUBLIC_FIREBASE_CLIENT_EMAIL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
}),
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
});
}
When I deploy it to vercel with environment variables as:当我使用环境变量将它部署到 vercel 时:
NEXT_PUBLIC_FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n"
I get this error message.我收到此错误消息。
> Build error occurred
Error: Certificate object must contain a string "private_key" property.
at FirebaseAppError.FirebaseError [as constructor] (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseAppError (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:122:28)
at new Certificate (/vercel/path0/node_modules/firebase-admin/lib/auth/credential.js:118:19)
at new CertCredential (/vercel/path0/node_modules/firebase-admin/lib/auth/credential.js:187:64)
at Object.cert (/vercel/path0/node_modules/firebase-admin/lib/firebase-namespace.js:220:58)
at Object.3996 (/vercel/path0/.next/server/chunks/241.js:115:76)
at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:42)
at /vercel/path0/.next/server/pages/dashboard.js:124:81
at Function.__webpack_require__.a (/vercel/path0/.next/server/webpack-runtime.js:103:13) {
type: 'FirebaseAppError',
errorInfo: {
code: 'app/invalid-credential',
message: 'Certificate object must contain a string "private_key" property.'
},
codePrefix: 'app'
}
Error! Command "npm run build" exited with 1
Error: Command "vercel build" exited with 1
However running npm run build
builds with no error locally.但是运行
npm run build
在本地没有错误。
.env.local
info - Linting and checking validity of types
info - Creating an optimized production build
info - Compiled successfully
info - Collecting page data
info - Generating static pages (4/4)
info - Finalizing page optimization
Page Size First Load JS
┌ ○ / (466 ms) 461 B 271 kB
├ /_app 0 B 270 kB
├ ○ /404 (450 ms) 526 B 271 kB
├ λ /api/hello 0 B 270 kB
+ First Load JS shared by all 270 kB
├ chunks/framework-a87821de553db91d.js 45 kB
├ chunks/main-567f0ec5ceee81fc.js 29.7 kB
├ chunks/pages/_app-b443b414c9e8f379.js 195 kB
├ chunks/webpack-42cdea76c8170223.js 1.07 kB
└ css/966e875c066cf46b.css 5.18 kB
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
○ (Static) automatically rendered as static HTML (uses no initial props)
I do not understand how to fix this because this runs locally and builds without any errors.我不明白如何解决这个问题,因为它在本地运行并且构建时没有任何错误。
I found the answer.我找到了答案。
.env.local
can have values like .env.local
可以有类似的值
NEXT_PUBLIC_FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n"
But in vercel environment variables add the keys without the double quotes ""
.但是在 vercel 环境变量中添加不带双引号
""
的键。
NEXT_PUBLIC_FIREBASE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n
When you add the keys it should reformat itself and look something like this.当您添加键时,它应该重新格式化自己并看起来像这样。 You can
console.log
the values locally, that will look like this below and paste the exact same value in Vercel env.您可以在本地
console.log
值,如下所示,并将完全相同的值粘贴到 Vercel env 中。
-----BEGIN PRIVATE KEY-----
your
secret
key
-----END PRIVATE KEY-----
I ran into this problem too.我也遇到了这个问题。 Removing the quotes didn't help this far.
到目前为止,删除引号并没有帮助。 However, my comment is related to a different thing: You are setting the private key to a NEXT_PUBLIC var (accessible via front-end, hence, exposed).
但是,我的评论与另一件事有关:您将私钥设置为 NEXT_PUBLIC var(可通过前端访问,因此公开)。
Just that.只是。 Sorry if it's not relevant.
对不起,如果它不相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.