When I try to import the library into my ReactJS app, I get this error:
underscore.js:15 Uncaught TypeError: Cannot read property '_' of undefined
The error is thrown here. root is undefined.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var root = this;
// Save the previous value of the `_` variable.
var previousUnderscore = root._; // <== !!! HERE !!!
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
Why does it happen? How to solve?
If you're transpiling with babel, or using "use strict", the value of this will always be set to undefined, which is why root is undefined. You need to do this differently. This is not a problem with underscore.