Browserify hello world example that exports a function for both browser and Node.js usage.
Basically answering: https://stackoverflow.com/questions/23296094/browserify-how-to-call-function-bundled-in-a-file-generated-through-browserify
Node.js development usage:
git clone https://github.com/cirosantilli/browserify-hello-world cd browserify-hello-world npm link npm link browserify-hello-world ./browserify-hello-world
Output:
1 2 3
Here, ./browserify-hello-world
is a Node.js executable, and it sees our browserify_hello_world.myfunc
function and calls it.
Node.js usage from NPM:
npm install browserify-hello-world npx browserify-hello-world
Browser usage:
npm run b
This runs:
npx browserify --outfile out.js --standalone browserify_hello_world index.js
which creates the file out.js
, which is used in the index.html example with:
<script src="out.js"></script>
You can observe it:
xdg-open index.html
That page contains 1 2 3
, which was also generated by our browserify-hello-world
code just like the Node.js call.
As you can see, the cool thing about Browserify is that it recursively packs up all Node.js dependencies into the browser version of the JavaScript. Here as an example we imported the uniq
library both on Node.js and browser.
Tested on Chromium 78, Ubuntu 19.10.