Viewers: in countries Watching now:
Many modules on NPM are available as command-line tools when you install them with the -g flag. With a few quick adjustments, it's possible to turn any Node project into a command. In this video, we'll make it possible to launch an existing Node project with a command. Let's go to the exercise files. Go to chapter eight, video four and copy the start folder to the desktop. Now go to terminal and let's install the project dependencies, change directory to that folder. I'm just going to drag it in to get that path, press Return and then type NPM install and then return.
So now that all the project dependencies are installed, I'm going to open the start folder in Sublime Text. First let's go to packaged.json. Currently this name is set to the default that's provided by the Express Command Line tool. We want a unique name, so I'm going to set this to airline. This is also going to serve as the name of the command that gets installed. And then also we need to add a property here. It's going to be called bin, and it's going to be set to the name of the script to run. Since Server.js is going to be executed directly by the terminal shell, we need to make a small adjustment to the file.
At the very top, we need to add a HashBang so that the shell knows to feed this entire file through Node. So let's do that now. Go to Server.js and just add that HashBang. Its going to be a hash and then an exclamation point. Then am going to type /usr/bin/env, then it's going to be a space and then node. This will ensure that the shell uses the correct node executable to run this script. Finally, we're ready to install this application as a command.
Since we're working with the project locally, we cannot use NPM-install-G to install the command. Instead, we're going to link the module. To do this, go to the root of the project in Terminal. So we're already there in the start folder. And to link this, we're going to type, npm space link, and then press Return. Now on my machine, I'm not an administrator, so it's giving me this error saying that you're not an administrator, and you don't have permissions to write to that folder. So to get around that I am going to add sudo to the beginning of this command.
So press up on the keyboard and then go to the beginning of that command and then type in sudo and then a space, now I am going to press Return and provide my password and now it installs that as a command. So, when I did that this created a symbolic link to the path where we have the project. So, be sure to put your your project in a place where it's not going to get moved. Now if you do have to move it, just run NPM link again in the new location. I'm going to close out of this terminal window, and I'm going to open up a new one.
Just so that I'm at my home folder. So now after I did that NPM link. I should be able to type airline and start the server. So now the server is listening on port 3000. And I can go to the browser and go to port 3000, and load up one of the flights. And everything is working. I'm going to go back to Terminal and close this out. So with a few small adjustments to package.json and some of your source code, you can turn any Node project into a command. The NPM link command will install the project as a global module.
In the next chapter we'll take a look at events and streams in node. In the meantime, the next video contains a challenge that will reinforce working with command line tools and
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.