Transcript
  • Meteor
  • Tutorial

Bundling

In this video, we'll look at the Meteor bundling process, which is a key step in preparing your application for deployment. I'll create a Meteor application named app and change into the app directory. And I can start up the application by typing meteor.

We're running our application in development. And to get a better sense of the processes that are running, let's open up another tab and take a look at all of the node processes.

You can see two processes are created as a result of running our application in development mode. The first process runs the Meteor.js script.

This is responsible for watching for file changes on our project and for re-bundling the application if any files change. It also restarts the server any time that there is an error in development.

The second process is our application. The script that's running in the process is a file called main.js, which is created automatically for us in the bundling process, which we'll look at in a second.

So these two processes work together-- the first one looking for file changes and automatically bundling our application if those files change, as well as making sure that our applications process stays alive. And the second is our actual application main.js file.

What we'll do next is we'll create a bundle manually by calling the Meteor bundle command. I can create a meteor bundle in my application directory by typing meteor bundle. It expects a bundle name, so I can call this bundle.tgz, for tarball gzipped.

The bundle is tarred and gzipped this way so that it makes it easy to send it up to a remote server. But since we're working locally, let's just untar this file immediately.

To do that, I can type tar with the x flag for extract, the z flag telling it that it's a gzip file. And the file that we'll extract is bundle.tgz. Now, we have a folder in our project directory called bundle. And we can change into that folder and take a look.

You can see a file named main.js, just like the file that was being run in development. This is the main file that we'll be passing to node to run. And you can see a programs and server folder and a star.json file. We'll get into the anatomy of a bundle in another video.

You can also take a look at the readme file that's associated with the bundle. And it'll give you some instructions for how to run your application.

Before we run our main.js file manually, you'll want to make sure that you've got node version 0.10.20 installed. And then using npm, make sure that you have fibers installed on the local machine with version 1.01.

You'll also want to make sure that MongoDB is running locally. And we can set our Mongo URL in a second manually to a local database.

And finally, we'll need to set a port, since we don't want the application to be listing on port 80 for our local machine. We'll just set it to 3,000, just like in development.

To start up our bundled application manually, I'll follow the instructions. First, we'll install fibers. It's already installed on my local machine, so this should work pretty quickly.

Next, I'll export some environment variables that will be used when we run our main file. So first, I'll export the Mongo URL. This tells Meteor how to connect to a Mongo database.

And for now, we'll be using a local Mongo database instance. And so all type MongoDB and then 127.0.0.1 for local host. And it'll be on port 27017. And the application database name, we'll just call app test.

Next, I need to export the port environment variable. And we'll set that to 3,000. As you'd expect, this tells Meteor what port to start up the application server on. Next, I can run the Meteor application by typing node and running the main.js file.

You can see the application server is listening. And it's running in our current shell. We can take a look at the processes that are running now. And we can see there's only one this time, main.js. So the first process there, named node main.js, is the application that we just started.

And you notice that there isn't a Meteor.js process running anymore to watch for file changes and so on, because we're assumed to be in production and now we're just running our application directly. Finally, I can open up a browser and confirm that the application is running on port 3,000 as well.

In this video, we looked at how Meteor runs our application in development by creating a build automatically that it puts into the dot meteor local build folder.

We also looked at how we can bundle our application manually by using the Meteor bundle command. And then untarring and unzipping the bundle.tgz file into a bundle folder. That looks very similar to the bundle folder created in development.

Finally, we looked at how we can start up our bundled Meteor application manually using Node.js by starting the main.js file of our bundled application and using environment variables to tell Meteor where to find our Mongo database and what port it should start on.