4

I am working on a web app that uses Webpack to bundle modules. For whatever reason, the introduction of Firebase into the app is causing Webpack to throw an error. This error is occurring when Webpack attempts to load the Firebase module.

How do I go about excluding Firebase from Webpack, but can still call

import Firebase from 'firebase';

from within my JS files?

Here are some screenshots of the error.

pic1 pic2

1

1 Answer 1

23

tl;dr Exclude /node_modules/ from the babel-loader paths.


Your 2nd pic shows the error on firebase-web.js:12:

Uncaught TypeError: Cannot read property 'navigator' of undefined

Unfortunately, firebase-web.js is minified, so it's hard to tell exactly what's going wrong. Let's beautify firebase-web.js using http://jsbeautifier.org:

TypeError in beautified firebase-web.js

Now it's plain to see that the script is trying to access aa.navigator, but aa is undefined. You can see at the top of the file:

var h, aa = this;

We can see what the script is trying to do now: it expects this === window so it can access window.navigator.

But why is this undefined? It's because, at some point, the script is being put into strict mode, which causes this === undefined instead of this === window. We can see that in the webpack-generated main.js:

"use strict" in main.js

It turns out that the "use strict" is being prepended by babel-loader, so we should be able to disable babel-loader for firebase-web.js to solve the problem:

...
module: {
  loaders: [
    {test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader'}
  ]
}
...

firebase-web.js without babel-loader

Good, now there's no more "use strict" and the error no longer occurs!

(Full disclosure: I worked on the same project that @kashiB is working on and have access to the source code.)

7
  • Thank you so much ... I have spending a week trying to figure out the error on window.navigator, on a another context but with babel. great !!
    – benek
    Oct 4, 2015 at 16:51
  • The tl;dr comment is what helped me. Turns out, I was doing this: exclude: '/node_modules/'. I removed the quotes and everything started working. Now only if I could get those 3 days back! Mar 23, 2016 at 3:46
  • I was using rollup and exclude: '/node_modules/**' alone did not fix the issue. I had to add useStrict: false to my rollup config also Nov 6, 2016 at 23:24
  • @lukejacksonn '/node_modules/**' is wrong. The slashes are the JS delimiters of a RegEx expression. If you add quotation marks (') you turn it into a regular string. 'node_modules' on the other hand should work as well as /node_modules/ does. Feb 22, 2017 at 21:47
  • I am a babel noob. I am working on a react-native project, what file I am placing this in? Jun 23, 2017 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.