possible way to use firebase-admin with Deno

Viewed 2232

Since Deno has been released stable build just a few days ago, Is there a possible way to use firebase-admin with Deno?

3 Answers

As @Evandro Pomatti pointed out, there is no official support from the firebase team as a native Deno module. However, NPM modules can be used within a Deno codebase, so why not just use the existing firebase-admin codebase?

see How to use npm module in DENO?

import { createRequire } from 'https://deno.land/std/node/module.ts';

const require = createRequire(import.meta.url);
const admin = require('firebase-admin');

const text = await Deno.readTextFile('path/to/serviceAccountKey.json');
const adminKey = JSON.parse(text);

admin.initializeApp({
    credential: admin.credential.cert(adminKey),
    databaseURL: 'https://databaseName.firebaseio.com'
});

const db = admin.database();
const ref = db.ref('restricted_access/secret_document');
ref.once('value', function(snapshot) {
    console.log(snapshot.val());
});

Since Deno is a secure runtime by default, reading files require explicit permissions using the --allow-read command

deno run --allow-read=node_modules myfile.ts
Related