Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Introduction to Browserify Part 1

4:36 Node.js lesson by

Browserify is a tool that brings node.js style development to the browser.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Browserify is a tool that brings node.js style development to the browser.

Avatar
Chris

slightly off topic but in the video Ben used the script: to type the tag into the html page. is this some sort of feature with webstorm?

In reply to egghead.io
Avatar
Carlo

Perhaps it's a silly question, but when I digit "browserify main.js > bundle.js" I get an error "browserify: command not found".

Otherwise everything else works, but from the error forward I'm unable to proceed.

:(

Browserify is a phenomenal tool that brings the common js and node-style workflow to client-side development. What we're going to do here is look at exactly how that works. I've got this entry file here which I just called entry.js because that's the common terminology for a starting point file in Browserify. We're requiring this dep file, and you can see this dep file simply exports a three element array of foo, bar, and baz. We're requiring that and then just logging that out.

You can see that just like any other node file we can run that with node, but if we want to be able to run this in the browser that's where Browserify comes in. what we can do is call Browserify. We'll pass it our entry file and then in this case, the simplest syntax is to tell it to output to bundle.js. Bundle is again, a common Browserify terminology that what it outputs is generally referred to as a bundle. You can see here that it has written bundle.js for us.

Right now it looks a little bit crazy because it's somewhat minified by default. But if we just format this code, you can see that it really just boils down to one function that is immediately invoked. It's an immediately invoked function expression which you may be familiar with. Then these are the arguments that are passed to that function. You can see here that this is the contents of our entry.js file here, and then here are the contents of our dep.js file. What you'll notice is that the contents of each of these files is now wrapped in a closure.

Our entry.js file here, the contents of it are wrapped in this function. You can see what's passed into this function is require, module, and export. Where those things would normally be provided by the node environment, in this case Browserify is providing them for us. Essentially it's just mapping each of these files to a lookup, so you can see that our dep.js file has been given the ID of 1, so we've got an object hash here. The ID of 1 maps to this file and its dependencies. The ID of 2 maps to what was our entry.js file and its dependencies.

You can see that our entry.js file has this dependency and it has mapped the ./dep string to the ID of 1 which matches up here. It's a little bit confusing and hard to follow, but you really don't need to. Browserify is very well covered with tests, it's used by thousands of people. You can rely on the fact that it's going to work. This function up here which really un-minified we're looking at 20 lines or so, is not that complex really. But the fact that it's minified makes it a little hard to read, but essentially all it does is it implements the lookup functionality to do this mapping of ID to the files themselves.

What we can now do is if we make this a little bigger, we can actually now run the node bundle file and we'll get the same output as we did before running the entry file. Of course since this is all a single bundle, you can then include that in an HTML file and get the same output. Let's go ahead and create a simple HTML file. If we go ahead and include that bundle.js file in our HTML file we can then go run that file and see that we get that output in our console in the browser that we did on the command line.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?