Rollup/bundling doesn't work properly with the admin SDK
nickdnk opened this issue · comments
- Operating System version: macOS 14.3.1
- Firebase SDK version: 12.0.0
- Firebase Product: unsure exactly which product is failing, but app and/or messaging
- Node.js version: 18.19
- NPM version: 10.2.3
Describe the problem
Steps to reproduce:
Configure a project for rollup, something like this:
{
"name": "project",
"dependencies": {
"firebase-admin": "^12.0.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.12.0",
"typescript": "^5.3.3"
},
"engines": {
"npm": ">=9.0.0",
"node": ">=18.0.0"
}
}
And in rollup.config.js
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
export default {
input: 'src/app.ts',
output: {
file: 'build/app.js',
format: 'cjs',
},
plugins: [typescript(), nodeResolve()]
};
And an app that does anything like this:
import { initializeApp, cert } from "firebase-admin/app";
import { getMessaging } from "firebase-admin/messaging";
initializeApp({
credential: cert({
clientEmail: fcmClientEmail,
privateKey: fcmPrivateKey,
projectId: fcmProjectId
}),
databaseURL: fcmDatabaseUrl
});
// and so on...
Then try to build with rollup -c rollup.config.js
, and you'll be met with:
src/app.ts → build/app.js...
[!] RollupError: "default" is not exported by "node_modules/firebase-admin/lib/app/index.js", imported by "node_modules/firebase-admin/lib/esm/app/index.js".
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
node_modules/firebase-admin/lib/esm/app/index.js (1:7)
So either I'm a complete moron (which is not unlikely), or something isn't right here. I've researched it for a while now, and it seems I'm doing what I'm supposed to. I'm trying to rollup/treeshake the node application to make it as small as possible for a serverless function.
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
This looks like a rollupjs config issue to me. Can you play around with the @rollup/plugin-commonjs
settings to see if that helps? Maybe start with requireReturnsDefault
. See https://stackoverflow.com/a/74126843/1526201