I wanted to try a node module in a browser app that uses global variables to share stuff.
Versions used below
I was thinking about using superagent to do ajax in a browser app that uses globals to access libraries and modules. The slightly opinionated superagent only supports CommonJS.
1. Install the node module
Create a package.json if there isn’t one already. This will be your record of what version of the module was used.
-f will accept all the default options of npm init.
Then install the module.
2. Create entry file
browserify and webpack will bundle dependencies of the entry file (and their dependencies, and dependencies of their dependencies, etc). In this case a dependency is anything pulled in through a
The entry file is just a CommonJS passthrough – it exports the module we want to use.
3a. Use browserify
The argument to
--standalone is the name of the global variable. It’s named
request below because that’s the naming convention used in the superagent docs.
3b. Or use webpack
The command I use for webpack is similar to browserify.
It will default to creating a global variable named by the argument to
--output-library. To generate a umd module or any other type, you’d also use
--output-library-target and specify the module type.
Now in my browser app I can do