So far, in order to start our applications, we've changed into the Meteor Application directory and then use the Meteor Command Line tool to run the application in development mode. In this video, I'm going to show you how to build your application manually and run it using NodeJS directly. This is something we would do, for example, if we were deploying our application and running it in production.
And so this build process happens in development automatically. And we can get a look at it over in the right where I've opened up another terminal window for us to explore the .Meteor private directory. So inside that .Meteor directory, if we were to navigate into it and take a look, you can see there's this folder called Local. And I can change into that and see another folder called Build. And that's where our build application is going. So let me change into there and show you the contents of the folder.
So this is the actual NodeJS application that is running. And specifically, it's this main file that's being run over here as our development server. And Meteor is really smart. Any time we change one of our application files, this bundle over here, this application directory, automatically gets recreated for us, and the application is restarted.
So next up, I'm going to show you how to create this application bundle yourself using the Meteor Build tool. From inside the Application directory to build this application into a Meteor bundle, what we can do is use the Meteor Build command. And what we'll do is pass it a path where we want the resulting folder to go. So in this case, I want it to go up into our Root Project directory into a folder called Build.
We can pass some options to build, and the one option I'm going to pass is called Directory. And this tells the Meteor Build Command Line tool that we want to output a directory instead of a tarball file. When that command finishes running and I change back into my Root Project directory, you can see now that there is a folder there called Build. And it's sitting next to the App directory.
If you navigate into the Build directory and take a look, you can see it has another folder called Bundle. And there might actually be additional folders in here that meteor puts for various things. So I'm going to change into the Bundle folder, and we can take a look. And notice that this directory looks very similar to the one we just saw in our Local Build folder for development, the difference being that we created this build ourselves instead of it being created for us by the Meteor Development tool.
The next thing you can do is to take a look at this automated Read Me that was created by Meteor. So we can use the cat command followed by the name of the file to take a look at its contents. And notice it gives us a description of what this folder is all about-- namely that it's a Meteor application bundle and the version of NodeJS that's required. It also gives us instructions on how to run the application manually using NodeJS. And then also how to change the port using an environment variable, or have it default to port 80 for most production systems.
So I'll follow these instructions almost exactly in order to run this meteor bundle from the command line using Node directly. So I'll type CD program server, and then we'll use the NPM command to install all the NPM packages that are required. This is nice, because it keeps all of the dependent NPM packages local to our Meteor applications bundle.
Next up, let's actually run our application. So to recall, if we take a look at the Project folder, what we want to do is to run the main JS file using a node. So we can use the Node program by typing node, and then the name of the file that we want to run-- in this case, Main.JS.
And before we do that, I want to set one environment variable right here on the command line called Port, which we would normally set inside of our env.sh file. But for now, just to get us started quickly, I want this to fire up on port 3000 instead of the default of port 80. So when I press Enter here, our application should start up. And we can access it in the browser just like we can in development. Except this time, we're running it directly using NodeJS.
Now let me show you a few tricks here. First up, let's take a look at our production environment variables. Back in our project, I created another folder under the Config directory called Production which contains an environment.sh file and a Settings file for production. Now, normally, this folder, you would just sim link to you on your production server. And I'll show you how to do that in a production class. But I want to give you a sense of it here.
So notice here is our env.sh file, and I've exported two sample environment variables. The first says to use port 3000 instead of the standard of 80 for production. And then I created the sample production variable with some random value so that we can see it.
Next up, we need to get the environment variables and Meteor settings into our environment. So normally, we would use some kind of shell script or tool to help us with this, but I'm doing it all manually so that you see how it works. Later on, I'll show you how we can use a start script to make all of this much easier.
So the first thing we'll do is use the source command and get our environment SH file loaded. So this, time it'll be in Config Production, and the file will be env.sh. Next up, we need to get Meteor settings loaded. Previously, we used the Meteor Command Line tool and then a Settings flag with a path to the settings. But this time, since we're using NodeJS directly, we need to load these settings into a special environment variable called Meteor Settings.
Let me show you a trick for how to do that. We'll use the Export keyword, just like we did in our env file. And I'll create a settings variable called Meteor Settings. And we're going to set that equal to the result of executing another command. So we're going to use the dollar sign here, and then we'll use the cat command to load up the file that's located in Config, Production, Settings, JSON.
And to see whether this worked, we can use the Echo command and then print out the Meteor Settings environment variable value. And there we have it. So it worked properly.
Next up, we can run our application from the Build folder using the node executable like this-- Node Build Bundle, and then the main file. Now our application is running using NodeJS, and our Meteor settings have been loaded successfully up here. And down here are all the environment variables that are visible to the server side of our application.
Notice that our custom production variable is defined here with its value. And we also have this port environment variable set to 3000 so we can get to our app on the browser by going to local host 3000. Also note that this node_environment short for environment has been set to Production instead of its normal value of Development.
One last trick I'd like to show you-- if we look at our node version right now, it's version 0.10.29. But as we upgrade to newer and newer versions of Meteor, sometimes, it will use a require later versions of Node. So we'd really like to be able to specify a version of Node to use when running our Meteor application.
To do that, we can use the NPM package that we installed earlier with N. And I'll type N Help to get a sense of what we can do here. We're going to use the N Use command so that we can use a specific version of Node to run our bundled Meteor application.
This time, instead of typing Node directly, I'll just type N Use and then the version that I'd like to use-- in this case, 0.10.29. And I got that version from the Read Me of my bundle. So then we'll pass it the path to the Main.JS file, which is in Build, Bundle, and then Main.JS. So now we're sure that we're using the right version of NodeJS and running our Meteor application from the bundle folder.
In this video, we departed from our normal workflow of running our applications in development mode, and I showed you how to both build and run your application directly from NodeJS. We also looked at how to use production-level configuration settings in our Environment and Settings file. Then finally, I showed you how to use the N NMP package to make sure that we're running our project with the correct version of NodeJS.