According to the browserify-shim docs, you can specify which globals browserify-shim needs to expose from your legacy modules by using the following syntax in your package.json:
{
"browserify-shim": {
"legacyModule": "myVar"
}
}
I want the legacy module to be accessible via require('legacyModule') and window.myVar.
From my experience, if the non-commonjs module I am trying to shim uses window.myVar = x or just myVar = x, the module is exposed globally and is available via require() as expected.
However, when the legacy module uses var myVar = x, that is what causes the problem as the module is then only accessible via require('legacyModule') and not via window.myVar.
The browserify-shim documentation states that:
Additionally, it handles the following real-world edge cases:
- Modules that just declare a
var foo = ...on the script level and assume it gets attached to thewindowobject. Since the only way they will ever be run is in the global context — "ahem, ... NO?!"