NPM scripts are a part of the Node.js environment. They require no additional installation and provide a conventional method for running local binary commands. Grunt tasks add a mature plugin ecosystem and are good at synchronous execution. The TodoMVC++ application will be leveraging both NPM scripts and Grunt tasks.
(intense music) - We did a little bit of hacking on express. Now we're gonna move into a part of the stack that we've already been using, but we're gonna try to dive in a little bit to understand better how these things work and what they can do for us. We've been executing these commands, these migration commands, these grunt commands, and these are provided by some of the build tooling that we have in this project. Now, build tooling, there's a lot of different choices out there, but the two that I keep coming back to are NPM scripts and Grunt, and Grunt tasks in general.
And I'll kinda take you through what each of these things do. So NPM scripts are commands that you register in the package.json of your node project. And can execute arbitrary shell commands. So it's essentially like adding aliases for commands that you could run from the command line yourself. However, there is a couple things that you get, get for free when you do an NPM script like this.
Probably the most notable one is that if you have NPM installed locally, any modules which should be command line utilities that have some kind of command line option, those utilities will be added to your system path when you run a script through NPM, through an NPM script. And we'll see what we mean by that here in a moment. It's also a conventional way of interacting with node projects. So, the NPM defines a set of, I don't know, about 15 different scripts that it sort of expects to possibly be present in a package.json.
There's an NPM start, an NPM test, an NPM after install. There's a few like lifecycle hooks and other bits that NPM will sort of conventionally expect to possibly be present in a package.json. So by conforming to that convention, another node developer who may or may not have ever seen your project before knows well, if I execute NPM start, like probably something is supposed to happen. In fact, lots of platform as a service providers, including elastic beanstalk which is the bit which is the bit that we'll be using later on this afternoon.
We'll look for that NPM start script as the primary way to kick off your node JS web application process. So having these NPM scripts is usually a good idea anyway. Then the other one that I, other one that I use is Grunt. And I'm already seeing some questions in the chat about, you know, Grunt over Gulp or whatever and we'll talk about some of the conventions. I think it largely does come down to preference.
Gulp in some scenarios, you know, performs really well. So that's really great. Webpack you can actually use with Grunt, and it's really good at doing like a subset of front end-y stuff. So there's lots of different choices out there, but the reason why I kinda stick with Grunt is the mature plugin ecosystem. There's a ton of plugins out there that do 99% of the things that I wanna do. And it's better than other solutions out there, I think, at like synchronically orchestrating tasks.
If you wanna do this, then this, then this, then this or do a couple things concurrently, and then another thing, Grunt makes that easiest. And there are other tools out there that is that's, all that stuff is still totally possible, but the code becomes, for at least my simple brain, possibly a little bit more confusing. And it's, you know, there are lots of people out there in fact, like, the Lloyd who lives here in town in the Twin Cities does a node devops for Walmart, and he, you know, he, for most of their orchestration stuff, his like weapon of choice is like bash scripts, like they just have, like a ton of like really bad ass bash scripts which like run all of their deployments and do all of that stuff.
So ultimately it comes down to preference, but the thing that I think Grunt helps with over just like pure bash scripts and NPM scripts is orchestrating. And like building tasks that work together.
Note: This course was created by Frontend Masters. It was originally released on 12/28/2016. We're pleased to host this training in our library.
- Serving HTTP requests with Express
- NPM scripts and Grunt
- NPM scripts and Elastic Beanstalk
- The database
- Sequelize and PostgreSQL
- Production environment
- Elastic Beanstalk and RDS
- Provisioning an environment
- Sass and Sass alternatives
- Building with Vue.js
- Real-time user interfaces
- Production monitoring
- Google Universal Analytics