Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node.js support for Cloud Firestore (to "firebase" SDK) #221

Closed
mikelehen opened this issue Oct 13, 2017 · 16 comments
Closed

Add node.js support for Cloud Firestore (to "firebase" SDK) #221

mikelehen opened this issue Oct 13, 2017 · 16 comments

Comments

@mikelehen
Copy link
Contributor

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.

@jshcrowthe
Copy link
Contributor

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

@mikelehen
Copy link
Contributor Author

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.

@jshcrowthe
Copy link
Contributor

SGTM 👍

@goldensunliu
Copy link

goldensunliu commented Oct 17, 2017

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.

@codasana
Copy link

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.

@adamduren
Copy link

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

@alexandregiordanelli
Copy link

Im facing the exact same issue too

@kuashe
Copy link

kuashe commented Oct 29, 2017

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

@olivierlesnicki
Copy link

+1

@adamduren
Copy link

@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.

@olivierlesnicki
Copy link

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

#221 (comment)

@chanlito
Copy link

chanlito commented Nov 7, 2017

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

mikelehen pushed a commit that referenced this issue Nov 16, 2017
* Fix miscellaneous node / GRPC bit rot.
  * Re-add grpc / protobufjs dependencies.
  * Remove crazy grpc stream error handling nonsense that is no longer
    necessary after grpc/grpc#9101
  * Clean up grpc_connection logging (and consistently use util.inspect()).
* Fix WebChannel / GRPC Connection compatibility issues.
  * Add an explicit mapping from "RPC name" (e.g. "BatchGetDocuments") to the
    REST url path (e.g. "batchGet") for WebChannel, and for GRPC just assume
    the first letter should be lowercased (e.g. "batchGetDocuments").
  * Split Connection.invoke() into invokeRPC() and invokeStreamingRPC(), with
    the latter accepting a stream of results and aggregating them into an
    array (needed to support BatchGetDocuments RPC).
* Fix serializer issues
  * Query limits are an 'Int32Value' but we were serializing them as a normal
    int which GRPC / protobufjs didn't like.
  * Several proto "oneof tags" were outdated.
* Add build steps to copy protos into npm package.
* Run integration tests for Node.JS
  * Added to 'test:node' script in package.json and in .vscode/launch.json
  * Include index.ts for browser and index.node.ts for node so the appropriate
    PlatformSupport gets registered.
* Misc cleanup
  * Remove unused MockPlatform since we use the normal NodePlatform now.
  * Remove 'google-auth-library' CredentialsProvider that we used to use for
    node.js (before we were integrated with FirebaseAuth).
  * Fixed several tests that were hitting node.js warnings about unhandled
    promise failures.
  * mocha commmand-line args:
    * "--compilers ts:ts-node/register" was deprecated in favor of
      "--require ts-node/register"
    * Remove "--retries 5" when running mocha tests from VS Code.
    * Consistently use "--require" instead of "-r"
    * Add "--exit" when running from VS Code.
@codasana
Copy link

codasana commented Nov 28, 2017

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

@mikelehen
Copy link
Contributor Author

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).

@codasana
Copy link

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

@CristianG540
Copy link

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

@firebase firebase locked and limited conversation to collaborators Oct 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests