ReferenceError: Can't find variable: require

firebase, javascript, node.js

Solution

You need to load those dependencies in your document so they are available to be required

I suggest you to use a module/dependency loader for the web like RequireJS

EDIT I no longer suggest to use RequireJS as the last commit on their repo is 6y old, and they have over 250 issues open.

Today we would use some module bundler to achieve this. There's many you can chose from: Webpack, RollupJs or Browserify among others. Google for "javascript module bundler" to get the whole list.

Problem

I have problem with loading `node_modules` to one of my webpage. I have npm(node.js) installed and I want to use `require()` function to init Firebase on my web and I don't why but it throw Reference error ReferenceError: Can't find variable: require. I am using bootstrap template and there are some files where the `require()` function works, so it's weird. Below the text is the command when I call function`require()`. (of course Firebase is saved in node_modules using ``` npm firebase --save` (`../node_modules/firebase`) ``` also tried something like `require('../firebase')` but there isn't problem): ``` <script> var firebase= require('firebase'); var config = { apiKey: "------------", authDomain: "------.firebaseapp.com", databaseURL: "https://-----.firebaseio.com/", storageBucket: "gs://----.appspot.com", }; firebase.initializeApp(config); </script> ``` Can someone help me to solve my problem? Thanks a lot!

Original source