How to use Node.js packages within a client-side html document using browserify

Viewed 303

I'm unable to use a node.js module on my website with broweserify. The example only shows how to run a javascript file that is separate from the .html file, not how to use the node.js module within the .html file. Seems like a trivial problem, but I'm unable to make it work.

Here's what I've done:

  1. Initialized node.js & installed a package, npm i webtorrent-health as an example
  2. Created require_stuff.js which consists of a single line: var WebtorrentHealth = require('webtorrent-health')
  3. Run browserify: browserify require_stuff.js > bundle.js
  4. Include package in my html document, e.g. <script src='bundle.js'></script>
  5. Use the package somewhere in my document, e.g. like this: <script>webtorrentHealth(magnet).then(foobazbar())</script>

Despite bundle.js executing and seemingly defining webtorrentHealth, the script within the .html document fails with WebtorrentHealth is not defined. What am I doing wrong? How do I make it work? Thank you!

1 Answers
Related