Skip to content

cirosantilli/browserify-hello-world

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

Browserify hello world

Browserify hello world example that exports a function for both browser and Node.js usage.

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.

About

Browserify hello world example that exports a function for both browser and Node.js usage.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published