Search code examples
firebasecloud9-ide

error 'Firebase is not defined' in online IDE (Firebase javascript client script-tag included)


I'm trying to follow the Web Quickstart on Firebase found here but I get stuck on step 2.

I set up a new webpage on Cloud9, using the HTML5 template, with following basic conent:

<html>
    <head>
        <script src="https://cdn.firebase.com/js/client/2.4.0/firebase.js"></script>
    </head>
    <body>
        "Hallo"
        <script>
            var Firebase = require("firebase");
            var myFirebaseRef = new Firebase("https://resplendent-heat-2801.firebaseio.com/");
            myFirebaseRef.set({
              title: "Hello World!",
              author: "Firebase",
              location: {
                city: "San Francisco",
                state: "California",
                zip: 94103
              }
            });
        </script>
    </body>
</html>

If I don't add the line var Firebase = require("firebase"); I get error

'Firebase is not defined'.

If I add the line I get the error:

'uncaught reference error: require is not defined.

I also tried to use a nodeJS template and then run command $ npm install firebase --save in the terminal. Result shows in terminal:

firebase@2.4.0 node_modules/firebase

└── faye-websocket@0.9.3 (websocket-driver@0.5.2)

(so installing firebase seems to work) and then add this page of code but that doesn't make a difference.

How to solve this issue? I do not have the option to install nodeJS locally (working on schoolcomputer).


Solution

  • Remove the require. Firebase loads correctly from CDN.

    Make sure you check security tab and add appropriate rules for write operations.

    <html>
        <head>
            <script src="https://cdn.firebase.com/js/client/2.4.0/firebase.js"></script>
        </head>
        <body>
            "Hallo"
            <script>
                //var Firebase = require("firebase");
                var myFirebaseRef = new Firebase("https://resplendent-heat-2801.firebaseio.com/");
                myFirebaseRef.set({
                  title: "Hello World!",
                  author: "Firebase",
                  location: {
                    city: "San Francisco",
                    state: "California",
                    zip: 94103
                  }
                });
            </script>
        </body>
    </html>