V
V
Vladislav Bezenson2015-12-09 23:57:27
underscore.js
Vladislav Bezenson, 2015-12-09 23:57:27

Why is "this" undefined in underscore after compilation after compiling browserify using debowerify?

Hello everyone)
There is a gulp task

gulp.task('js', function() {
    browserify('./src/js/main.js')
        .bundle()
        .on('error', onError)
        .pipe( source('main.js') )
        .pipe( gulp.dest(path.build.js) );
});

and this package.json:
{
  "browserify": {
    "transform": [
      ["babelify", { "presets": ["es2015"] }],
      "debowerify"
    ]
  },
}

In main.js, I import backbone (or underscore separately, it doesn't matter):
import Backbone from 'backbone';
And after compilation, the following is obtained in the console. mistake:
Uncaught TypeError: Cannot read property '_' of undefined

Checked with Chrome DevTools. The root variable is undefined at the beginning of the underscore library:
// 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._;

The most interesting thing is that if you compile without debowerify using npm instead of bower, then everything works. But i want to use bower.
Has anyone set this up and knows the ins and outs?
Thanks in advance :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question