Firebase Admin Nodejs cannot find module service_account.json

Zik picture Zik · Apr 2, 2017 · Viewed 8.7k times · Source

I start my node with "node firebasedb.js". My firebasedb.js contains this code:

var admin = require("firebase-admin");

var serviceAccount = require("service_account.json");

// Initialize Firebase
var config = {
    credential: admin.credential.cert(serviceAccount),
    apiKey: "<api key>",
    authDomain: "<auth domain>",
    databaseURL: "<database url>",
    storageBucket: "<storage bucket>",
};

admin.initializeApp(config);

When I run node, I am in the directory where the .json file exists. But it says

Error: Cannot find module 'service_account.json'

Answer

cartant picture cartant · Apr 2, 2017

You are missing the relative portion of the required path. That is, you should doing something like this:

var serviceAccount = require("./service_account.json");

If it's not a relative path, require will be looking in node_modules for a module named service_account.json.