
Introduction to Node.js, v2 Running npm Scripts
This course has been updated! We now recommend you take the Introduction to Node.js, v3 course.
Transcript from the "Running npm Scripts" Lesson
[00:00:00]
>> Did I mention that or I don't think I mentioned this? But you can also do multiple installs at one time. So, I don't think I mentioned that. So if you wanted to install npm, install lodash, express, react, typescripts. Just all these things, you can do them all at the same time.
[00:00:22] And this is where you'll get these long npm installs. I mean, I've literally had npm installs where I'll just walk away, go watch some anime, come back, check on it, walk away [LAUGH]. Sometimes it takes forever, depending on where you're installing from. Some internal things, some proxy or whatever.
[00:00:39] In this case is was really fast, I'm going really fast Internet and things are going really good. But yeah, you can install as many as you want and the same goes for uninstaller. So, I'm gonna uninstall all of these and it works pretty much the same. Yeah, I think that's pretty much really all you need to know about installing and uninstalling npm.
[00:01:01] Now when it comes to running the scripts. So let's say we have this test script here, right now it just says echo, no error, no test, specify exit 1. So, if you wanna run a script, you can type in npm, followed by the run keyword, followed by the name of the script.
[00:01:19] The only one that doesn't have to be prefixed with the run keyword, is test. So, you don't have to put run on test, you can actually just say npm test. And as you can see, it echoed out error no test specified exit 1, right. So, cuz that's exactly what's here.
[00:01:33] It exited 1, remember I told you about how the process could exit with a 1. That's where this came from. Exit 1 means error, that's what this is. This is an error. So, if you were to listen to the standard error to standard out, you would see an error there.
[00:01:52] You could pipe into that and write it to a log or something like that because it's an exit of 1. Obviously, we can change the test script to be whatever we want. And we will get into testing, so you'll see how that works. But yeah, that you can change it.
[00:02:04] So, we can also add other things here. So, if I wanted to add, I don't know, I wanted to add something that removed a file. So, I'll say, I'm gonna add a script called remove. And it's going to, actually let's make one called serve. So, I'll make one called serve.
[00:02:23] And it's going to open this index.html and serve it on the browser, right? So, first I need to find a package that will serve. I know one off the top of my head it's literally called serve, some npm install serve. And I'm gonna save that globally, actually. Someone's gonna say serve, like that.
[00:02:45] So while that's installing, I'm gonna go back here and I'm going to say, I'm going to use the serve cli. And I'm pretty sure I can point it to a folder. And it'll find the index.html, but it'll serve it for me. So, I'm gonna just do that. And let's see if that's done, that seems to be done.
[00:03:02] Now I can serve an npm, run, serve, and boom local host 5000 and it could not be found because I clearly didn't set it up right and I didn't look at the documentation. But that wasn't the point to get the serve, right? It was to show you that you can pretty much customize how this works, you can literally do whatever you want.
[00:03:24] You can write bash script here, you can put a JavaScript file here, and have it execute a JavaScript file if you wanted to. So, you can literally do whatever you want. And these scripts are very powerful. So, if you'd like, hop into like a big repo at a big company.
[00:03:38] Don't be surprised if you see like 20 scripts here that are doing a whole bunch of crazy stuff, because that's just how it is. npm serve, let's look at that, right click. We got here, so the way serve works is, serve a folder name.
>> I think it also plays a role for them and I think this was to ensure to react and not just central.
[00:04:00]
>> Yes, this is not central. Let's just let's do it the long way. So we'll say, serve like that and let's see what happens. Run that, let's see what the network says. Network says, Come here. No request, there we go. 404 not found, okay, let's try. Let's try, Let's just literally load up that thing like that and see what happens.
[00:04:36] So, see that. You know what, it's because I'm in the package directory, right? So, I got a CDO. So, there we go. Now, that should work. Let's try it now. So, do npm, run serve. Yeah, see, the problem is I'm in a package.JSON that's in a package folder, but I'm trying to run it on a sibling folder, right?
[00:05:03] So, this package.JSON would have to exists on a higher up. So, what I was gonna do, I'm just gonna just bring this over here, like this. There we go.
>> You just do dot dots slash.
>> Or I can do dot dot slash shot, I guess that will work too.
[00:05:19] So, yeah, obviously that's the easy answer. But all right, so we just did that. Let me go back into package. And then we could do npm run serve. Now we can go here and it still doesn't wanna work because I put the wrong folder, it should just be serve now.
[00:05:41] Now we've run serve, we go back. There we go, okay. So, now you can see how you're able to create pretty much whatever script do you want in the package.JSON and just prefix it with npm run, whatever you want and it works. It works pretty good and you can also see how important it is to read the documentation.
[00:06:03] Even if you've already used the thing 1000 times, and I use serve all the time. So, cool. I think that's pretty much what I want to cover with packages. Any final questions on that before we move on? The question was, can you specify additional parameters to commands inside the scripts?
[00:06:24]
>> Yes.
>> Yes, you can. So, let me write another one. So, if I say, What's a good way? Try to think of a good one. Yes, you can totally do it. I'm trying to think what's a good example at the top of my head. Let's try this, so if I make one called remove, and I use rm-rf, and then it takes a argument.
[00:06:50] What folder do you wanna actually or actually let's do a file? What file do you wanna move? So I can say, cuz rm actually removes a file, that's what rm was gonna do. So, I can say npm run rm and then followed by the file name. In this case, let's remove this index.html.
[00:07:09] Let's do that. So, we'll say remove index.html. So, if I run that, you can see I was able to pass an index.html to the rm command here as a argument. And it was actually able to execute and remove it from here. And the same thing goes with flags.
[00:07:30] So, if you had a flag that you wanted to pass in here, like rf, obviously this is going to break now. Let me make a folder so we can see that. So we'll make a new folder, call it hello. Now, if we wanna remove that folder, I could say remove rf which means recursively remove this folder and I call it hello.
[00:07:52] Hello, is a directory, okay? So I think you actually have to do like that. So, let's check that out. Okay, now the problem is, if I do that, I can't pass an argument. So, now I'm starting to remember. Yeah, you can't pass in an argument with a flag unless you hard code the argument here first.
[00:08:14] So, it's either or. So, there's not a lot of flexibility in it. So, if I wanted to do that, I'll have to put the name of it here. So, hello and then now I could use a flag because I have this slash here. Which tells us, hey, whatever other flags they want to pass in do that.
[00:08:32] But if I prefix that, then now I'm not able to pass in an argument which would be in this case be a folder name. So, in this case what I like to do when it gets that complicated, is that I would point to something like this. I would say some file js here, like run that thing, do that thing.
[00:08:52] And then in that file, I'll do the stuff that I wanna do. And you can use whatever npm module you wanna use, whether it's shell js, or custom bash scripting, a child process. Whatever you wanna do to run a bash script if you want. And that's what I do, I like to keep these pretty simple when they started getting complicated with all the arguments and things like that.