You can achieve that by using browserify-shim. Assuming that you’ve got a module named mymodule.js
that depends on jQuery in the global scope with the following contents:
var $ = require('jQuery');
console.log(typeof $);
-
Install browserify-shim:
npm install browserify-shim --save-dev
-
In package.json file, tell browserify to use browserify-shim as a transform:
{ "browserify": { "transform": [ "browserify-shim" ] } }
-
In package.json file, tell browserify-shim to map jQuery to the jQuery in the global scope:
{ "browserify-shim": { "jQuery": "global:jQuery" } }
-
Run browserify
browserify mymodule.js > bundle.js
If you examine bundle.js you will notice that require('jQuery')
is replaced with (window.jQuery)
.