Firebase-js-sdk: Add node.js support for Cloud Firestore (to "firebase" SDK)

Created on 13 Oct 2017  路  16Comments  路  Source: firebase/firebase-js-sdk

Currently to use Cloud Firestore from node.js you can use the "firebase-admin" SDK (see node.js getting started snippets at https://firebase.google.com/docs/firestore/quickstart).

But if you want to use the "firebase" SDK instead (e.g. so you can use Firebase Authentication), you are currently out-of-luck. The "firebase" npm module will not work from node.js and you may get errors like:

(node:15795) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: k.setTimeout is not a function

This is a known issue and something we intend to address in the future.

firestore

Most helpful comment

pretty sure this webpack error is also related to the lack of universal support for the 'firebase/firestore'

Cannot find module 'grpc'
Error: Cannot find module 'grpc'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/sliu/workplace/spotifly/node_modules/@firebase/firestore/dist/cjs/src/src/platform_node/grpc_connection.ts:32:14)
    at Module._compile (module.js:624:30)
    at Module._compile (/myproject/node_modules/source-map-support/source-map-support.js:492:25)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)

If you are using a bundler like webpack to build your client side bundle, you will need to load firebase/firestore on the client side only. luckly this can be done, however whenever you are about to access the .firestore feature of your firebase app, you will need to make sure the split bundle is loaded otherwise the name space will be null. here is an example

// since you have initialized your app somewhere above
export const getFireStore = async () => {
    await import('firebase/firestore');
    return firebase.firestore();
}
// and you can use it else where like
(await getFireStore()).collection('your-collection')

notice I gave this example with await in ES7, but you get the idea of how you need to resolve the promise before you can proceed.

All 16 comments

Does it makes sense to post this in #198 and rename that issue instead of duplicating?

Ahh, I scanned the issue list but missed that one. I'd rather use this as the canonical tracking bug, so I've closed #198 in favor of this. Thanks.

SGTM 馃憤

pretty sure this webpack error is also related to the lack of universal support for the 'firebase/firestore'

Cannot find module 'grpc'
Error: Cannot find module 'grpc'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/sliu/workplace/spotifly/node_modules/@firebase/firestore/dist/cjs/src/src/platform_node/grpc_connection.ts:32:14)
    at Module._compile (module.js:624:30)
    at Module._compile (/myproject/node_modules/source-map-support/source-map-support.js:492:25)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)

If you are using a bundler like webpack to build your client side bundle, you will need to load firebase/firestore on the client side only. luckly this can be done, however whenever you are about to access the .firestore feature of your firebase app, you will need to make sure the split bundle is loaded otherwise the name space will be null. here is an example

// since you have initialized your app somewhere above
export const getFireStore = async () => {
    await import('firebase/firestore');
    return firebase.firestore();
}
// and you can use it else where like
(await getFireStore()).collection('your-collection')

notice I gave this example with await in ES7, but you get the idea of how you need to resolve the promise before you can proceed.

We are building an app with Nuxt.js and facing the exact same issue. I've tried the temporary solution suggested by @goldensunliu but haven't got success.

Eagerly waiting for this issue to be resolved.

We're having the same issue trying to run tests against Firestore and our custom API wrapper using Jest.

Im facing the exact same issue too

Same issue too , tried @goldensunliu solution but didn't work out. Any other temporary solution ?

+1

@alexandregiordanelli firebase-admin does include firestore as noted in the original issue report. Here is the reference documentation.

Also, please try to be mindful of the community rules and keep the conversation constructive.

@adamduren It is indeed included. I believe the issue @alexandregiordanelli is facing (like the rest of us) is the one described by @goldensunliu

https://github.com/firebase/firebase-js-sdk/issues/221#issuecomment-337103110

Tried the solution above still getting, Error: Cannot find module 'grpc'

This issue has been closed but the problem is not resolved. I still get the error firebase.firestore is not a function. Isn't it related to this? I am using this package - https://www.npmjs.com/package/firebase - version 4.6.2

Update: After importing firebase, if I add require('firebase/firestore'), I again start getting the error - Cannot find module 'grpc'

Update: I have create a stackoverflow question describing steps to replicate the issue - https://stackoverflow.com/questions/47532667/node-js-support-for-firestore-not-working

Sorry for the confusion. This was fixed just recently and will be included in the next Firebase release. You can monitor the release notes (https://firebase.google.com/support/release-notes/js) to see when it has gone out (likely this Thursday).

@mikelehen Thanks for clarifying. I look forward to the next release :)

i麓m using webpack and i tried this "https://github.com/liady/webpack-node-externals" and read this "https://jlongster.com/Backend-Apps-with-Webpack--Part-I" and works great

Was this page helpful?
0 / 5 - 0 ratings