mtmacdonald
mtmacdonald

Reputation: 15070

How to use a CommonJS dependency without Browserify?

I want to use a JavaScript library in a web application which is not a single-page app and does not yet use a module loader.

But the library (virtual-dom) is only published as an npm package for Browserify, and all the examples use require.

How can I use a CommonJS dependency in a client-side app that does not use Browserify or a module loader?

Upvotes: 0

Views: 217

Answers (2)

Vic
Vic

Reputation: 8981

Easy, simply download this file here:

https://github.com/Matt-Esch/virtual-dom/blob/master/dist/virtual-dom.js

and then include it in your page:

<script src="virtual-dom.js"></script>

Now you can access it like:

virtualDom.create(...);
virtualDom.diff(...);
virtualDom.h(...);
virtualDom.patch(...);

Upvotes: 3

mtmacdonald
mtmacdonald

Reputation: 15070

The Browserify --standalone option uses "a window global if no module system is found".

The virtual-dom/dist directory is built with the command browserify --standalone virtual-dom index.js > dist/virtual-dom.js and can therefore be used in this way.

Upvotes: 1

Related Questions