3

I'm using browerify with browserify-shim to us the version of underscore and jQuery defined by the environment it's running in. I found myself requiring the same modules over and over again, so went to look for a way to automate that. The option opts.insertGlobalVars does it, but can I make it work with browserify-shim?

package.json

"browserify-shim": {
   "jquery": "global:jQuery",
   "underscore": "global:_"
}

gruntfile.js

browserify: {
  options: {
    browserifyOptions: {
      transform: ['browserify-shim'],
        insertGlobalVars: {
           _: function(file, dir) {
             return 'require("underscore")';
           }
        }
     }
  }

The _ get recognised as variable and an attempt is made to define it. Unfortunately this results in the file not being found. Since I don't include the file myself I can't provide a path or than in the example above.

Error: Cannot find module 'jquery' from 'path/to/file'

Edit: using require('underscore') directly inside my module does work.

Tim
  • 1,680
  • 2
  • 15
  • 21
  • Looks like `insertGlobalVars` cannot take non-word chars. Look at this line https://github.com/browserify/insert-module-globals/blob/a810104d4ef38ea46826438e2afbef1c0eedebbb/index.js#L75 – FreeLightman Apr 28 '18 at 17:16

0 Answers0