I am trying to deploy a NEXT JS app that uses firebase-admin on Vercel.
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:
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.
.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
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 ""
.
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.
-----BEGIN PRIVATE KEY-----
your
secret
key
-----END PRIVATE KEY-----