The new documentation provides a straightforward method of initialising firebase, for example, this snipped comes directly from the README.md file:
Install the Firebase npm module:
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.intializeApp({ ... });
// ...
But in my installation this is not working, as in, firebase is empty ({} when console logged).
There's another section in the documentation called Include only the features you need, which provides the following snippet:
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
var app = firebase.initializeApp({ ... });
// ...
This method works for me, so I'm wondering what could I be doing wrong in the first instance?
I'm using browserify, if that helps, but the docs state: The browser version is designed to be used with
a package bundler (e.g., Browserify,
Webpack).
Related
I need to use nodejs library in my angular project but got example code in nodejs. I have installed library through npm command and import to my component. But it always show undefined.
npm install phenix-edge-auth --save
const TokenBuilder = require('phenix-edge-auth');
// Create a token to access a channel
const token = new TokenBuilder()
.withApplicationId('my-application-id')
.withSecret('my-secret')
.expiresInSeconds(3600)
.forChannel('us-northeast#my-application-id#my-channel.1345')
.build();
How to use this code with angular app?
Import it as angular way..
import * as TokenBuilder from 'phenix-edge-auth'
I'm running a simple node app in my local machine and I need to connect to firebase realtime database.
I installed firebase via npm:
npm install firebase --save
Then I initialize the app:
var firebase = require("firebase");
var config = {
apiKey: "api-key",
authDomain: "my-app-database.firebaseapp.com",
databaseURL: "https://my-url.firebaseio.com",
storageBucket: "my-app-database.appspot.com",
};
firebase.initializeApp(config);
var myRef = firebase.database().ref("collection").on("value", (snap) => {
// do something with the data
});
Then I get the error that database is not a function. I check firebase.database and is undefined, also so are firebase.auth and firebase.storage.
I followed all the steps in the docs, but I don't see anything that could be causing this.
Goodness gracious... It was as simple as requiring the other packages in the file, like this:
// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");
Nowhere in the docs or reference says that. I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this:
https://www.npmjs.com/package/firebase#include-only-the-features-you-need
Just scroll down where they mention using npm packages or Typescript and you'll find the answer.
Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it.
I'm trying to see if it's possible to integrate Cloud Functions for Firebase with Braintree. I created a project for Cloud Functions according to the docs.
In the project directory I ran: npm install braintree.
I modified index.js for testing purposes to be the following:
const functions = require('firebase-functions');
var braintree = require("braintree");
var gateway = braintree.connect({
environment:
braintree.Environment.Sandbox,
merchantId: "useYourMerchantId",
publicKey: "useYourPublicKey",
privateKey: "useYourPrivateKey"
});
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-
functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
//gateway.clientToken.generate({}, function (err, response) {
//response.send(response.clientToken);
//});
});
When I tried to deploy this test function I got the error
Error parsing triggers: Cannot find module 'braintree'
I'm new to Firebase, Cloud Functions, and node.js and would appreciate any input on how to import Braintree to Firebase Functions project.
It looks like Cloud Functions for Firebase is not picking up the braintree module. Like most Node.js environments, Cloud Functions reads the dependencies from package.json. When you install a module with npm you can tell it to also write it to package.json by adding --save to the command line. So:
npm install braintree --save
You have the Node.js package braintree missing.
Your Firebase project has a directory called functions.
In the terminal, go to the functions directory by $ cd {your project dir}/functions
And then npm i braintree --save.
I hope that it helps you.
change current directory to functions:- cd functions
then install braintree in that folder using npm: - npm i braintree
then import braintree : - var braintree = require('braintree');
now everything should work fine.
Note:- if you have not enabled payment in firebase, it will though 'unexpectedError' in the http response.
I have a meteor app with a REST API by https://atmospherejs.com/simple/json-routes.
Now I want to document my API with SwaggerUI. I already used SwaggerUI in other projects and know you have to create a yaml or json sepc file, which then is being displayed by SwaggerUI.
So now I have discovered there are some existing swagger-ui packages for npm:
https://www.npmjs.com/package/swagger-ui
https://www.npmjs.com/package/swagger-ui-dist
which I installed by meteor npm install swagger-ui-dist --save-dev
But I have no clue what to do next or how to use them.
I am sure it must be something as simple as:
const swaggerui = require('swagger-ui');
swaggerui.specPath(pathToYaml);
swaggerui.url('/api-docs');
Can anyone help me with the first steps?
I am trying to access Realtime Database using the new feature introduced with 3.1.0 release: "The Node.js SDK now supports unauthenticated access. If no service account is provided, Realtime Database access will be restricted just as any unauthenticated client would be."
The SDK is updated to 3.1.0:
user#ha:~/dev/project/auth/firebase$ sudo npm install -g firebase
[sudo] password for user:
/usr/local/lib
└─┬ firebase#3.1.0
Tried with no service account:
var firebase = require('firebase');
console.log('Initialise Firebasse app');
firebase.initializeApp({
// serviceAccount: "",
databaseURL: "https://some-valid-firebase.firebaseio.com"
});
The result is:
user#ha:~/dev/project/auth/firebase$ nodejs fb_anon.js
Initialise Firebasse app
/home/user/dev/project/node_modules/firebase/auth-node/auth.js:61
throw new Error('Invalid service account provided');
^
Please help, probably I am missing something obvious here. :-(
What is happening is that your application is referring to an old sdk version that is inside your node_modules. And thats because you updated the firebase sdk with the globally flag and you are referring to a local sdk.
You should be installing it locally to your application.
First make sure you added "firebase":"3.1.0" into your package.json file and then call npm install inside the same dir.
I've just tested it with the same piece of code you added and everything went as expected.
In this question you can find some additional information and tips when using npm.