Check out a free preview of the full Introduction to Node.js course:
The "CLI with Node Exercise" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to complete a program that saves and retrieves contacts from an address book.

Get Unlimited Access Now

Transcript from the "CLI with Node Exercise" Lesson

[00:00:00]
>> Scott Moss: So now that we're done with that, we're gonna talk about Exercise 3, and you're gonna be creating a CLI program. So CLI program is short for command line interface, very similar to all the tools we just used now. When we typed in node or we typed in NPM, or we typed in YARN, those are all CLIs.

[00:00:15] You're gonna create one of those, you're gonna make your own. And basically what it's gonna do is it's gonna save and retrieve contacts from an address book. The program is partially there, so it's almost done, but you must install some remote modules and use the file system to get things to work.

[00:00:30] So there's gonna be some modules in there that you have no idea how to use, and I built the app in a way that you don't need to know how they work. Honestly, the work there is already done for you. If you're curious of how they work, you can go look them up and I can show you on the solution of what they're doing.

[00:00:44] But most of the time I built it so you don't need to know what they're doing. So just get that part out of your mind, like I have no idea what that thing's doing? That's fine, you don't need to know. You just need to install it, [LAUGH] and let it do its job.

[00:00:54] And then you need to do some stuff with the file system, so I'm gonna walk you through it. But basically, checkout to the start branch, again, if you have it, if you're on another branch. Install all your remote modules. I just showed you how to do that with NPM, so just make sure you install everything.

[00:01:06] You can just do that with one command with NPM. Check the README on how to run your CLI. There's some instructions in there on how to run it. And then you need to fix the CLI so that the new command works by saving contacts to the contacts.json. And you need to fix the CLI so that the list command works by listing all contacts and allowing you to select one, then it prints out the selected contact.

[00:01:29] So I'm gonna walk you through all that cuz that just sounds like crazy stuff. So let me just stash. All right, now I'm good with this, hold on. Yeah, we're good. So if we go look at the exercise, if you look at the README, the README has more instructions on here.

[00:01:45] So right here, the CLI. The location is exercises/cli. These are commands that you can run for this exercise. So always have these in the README, put this on preview.
>> Scott Moss: There we go.
>> Scott Moss: So we're gonna run these two commands. So if you type in this node exercises/cli/index.js, why did it do that?

[00:02:11] New, you run this command, it'll try to run that CLI command, just like if you type in npm install, this is running your CLI, which doesn't have a name yet and then the new command on it. I created a command called new. If you want to run the list command, you would type this in followed by the command called list.

[00:02:29] Because the CLI doesn't have a name yet we have to type in this full path and put node in front of it. If we were to package this up, we would give this CLI a name like address book, and then we can say address book new, address book list.

[00:02:42] But we're not doing that. That's complicated, that's later on. We're gonna just do what we know how to do, we put node in front of a JS file, we execute it. The only difference that we're doing now is we're passing an argument called new, that's it. And you're gonna see in the code how we use something like process to get that stuff, and there's a lot of tools in there.

[00:03:01] So you got to get these two commands to work. The new command,
>> Scott Moss: When it works, it's going to ask you a couple questions actually in a prompt about a first name, a last name and a phone number, and it's gonna save it to a file. The list command is just going to list all the contacts in a file.

[00:03:19] Once you got those two things working, you're good. What you need to fix is going to be this. So we're going to exercises > modules,
>> Scott Moss: cli, there's a couple things in here. So one, you'll see a contacts.json, your's will probably just be like an empty, or actually it might have some of these stuff already in it.

[00:03:36] If not, it doesn't matter, as long as there's a JSON object in there you're good. And the file has to be there, which it should be, so you should be good. This is just some fake stuff in there. Then you'll see an index.js. This is the code that I say doesn't really matter.

[00:03:50] So once you get to the point where you just install modules all the time, you have to let the code do its job and just trust that it's gonna do its job. And if it doesn't do its job, just don't use that module. So trust that this code's gonna do its job.

[00:04:04] The thing that you're gonna have to do is really nothing in this file. This file's pretty much good to go. But there's some modules here, you can see it says install any missing modules. There's some modules here, commander, inquirer, that just might not be installed, so maybe just install those if they're not there.

[00:04:21] Otherwise you'll get an error saying can't find this module, and you'll know exactly what to install. But what you're going to be doing is in the utils folder there are two functions, getContacts and saveContacts, and there's descriptions above them. getContacts should read the contacts at the contactsLocation path, which is here, and convert it to a JavaScript object.

[00:04:42] So this is a JSON file, so you need to convert it to JavaScript. I know you know how to do that if you write JavaScript. There's a function that converts JSON to JavaScript and vice versa. So you need to do that in getContacts and then return that object, and saveContacts does the opposite.

[00:04:54] It takes a contacts object, converts it to JSON and saves it to the,
>> Scott Moss: contactsLocation path. So you're gonna have to use the file system for this. So I talked about file system, write file and read file, I can bring that example back up. We're most likely going to have to use those to write and read from the file system.

[00:05:16] And for now just use the synchronous version of those. If you want to get Async and get fancy, be my guest, but I would stick with the sync methods for that. And that's what you gotta do, and once you do that, again, run those commands in the README, that look just like this, node exercises/cli/index.js new, node exercises/cli/index.js list.

[00:05:38] And you'll know when they're working, cuz they won't error out. [LAUGH] If they error out, then they're not working. So that's what you're gonna be doing. So it's 2 o'clock now, we'll come back at 2:30 and we'll continue to move. And I'll go back to the example of the file systems, I'm sure everybody probably wants to see some of that, that I might have had.

[00:05:58] So you had something like fs.writefile.
>> Scott Moss: Sync, and again, the documentation, Node documentation's a great place for this. I need fs.read, I'm not doing anything that's not there, readFileSync. So write writeFileSync takes in a path somewhere, so like lib.js,
>> Scott Moss: And then the string to save, right? And then readfile takes in a path somewhere.

[00:06:30] And that's it, it'll return it. You can probably get away with this exercise with just those two methods.