Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done! You have completed Using npm as a Task Runner!
You have completed Using npm as a Task Runner!
Preview
In this video we'll take a brief overview to on task running with npm.
Download the project files to follow along with this workshop
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[SOUND] Hello there, I'm Andrew, and 0:00
welcome to this workshop on using npm as a task runner. 0:04
A task is something that you need to do. 0:10
If you want to perform that task over and over again, 0:13
you'll save yourself a lot of time if you alternate the process. 0:16
Common web development tests include running test suites. 0:20
Compiling SaaS, TypeScript and CoffeeScript files. 0:23
Downloading assets of files from an external source that your package needs. 0:26
Starting a web server. 0:31
And starting a worker that goes through a queue of jobs, 0:33
like sending out emails or push notifications. 0:36
Popular jump script build tools like grunt and group are good for complex projects. 0:40
But you don't always need that power or 0:46
want to bother with the time consuming set up process that they require. 0:48
You can always turn to a full feature build system 0:53
later as your project evolves. 0:56
npm tests are called scripts, and 0:59
they're added to the scripts property of a project's package.json file. 1:01
Tasks have a name and a command, which is simply a string that's 1:07
executed on the operating system's command line when the task is run. 1:11
For example, you could echo out a message to the terminal, start up a web server, 1:16
or use a command line like mv to move files, or cp to copy files. 1:22
I'll show you a few examples in a moment. 1:28
Once you have the script added to the package.json file, you can run it simply 1:32
by typing npm run, the name of the script in your computer's command line tool. 1:36
For example, if you had a task named test, you'd run it by typing npm run test. 1:43
Or if I had a script named compile, that compiles CSS files, 1:51
I'd run it with npm run compile. 1:55
I'll focus on two types of task with MPM. 1:59
Default, or built-in tasks, and arbitrary tasks. 2:01
Built-in tasks are those that are common to most projects. 2:06
An example of a built-in task is "test". 2:11
Built-in tasks do not require the run command. 2:14
So npm run test can also be ran as npm test. 2:18
That saves a couple of keystrokes every time you want to run your tests. 2:24
Arbitrary tasks are those that you can name yourself. 2:28
They require npm’s run command. 2:32
Since there's no built-in compile task, if you added your own 2:35
script with that name, you'd read it like this, npm run compile. 2:40
Not all JavaScript applications will need custom or arbitrary tasks like this, 2:46
since the built-in task cover many of the most common situations. 2:52
Here's the simple project we're building in this video. 2:56
Dice Simulator 2015 is the latest craze. 3:00
You click the button and it rolls the dice. 3:03
This project is a front end project. 3:06
When I say we'll be building this, I don't mean that we'll be programming this. 3:09
All the programming has been done, but 3:14
we'll be getting the project ready to deploy. 3:16
Open up the associated work space with this video and we'll get going. 3:19
Let's take a look at the start point of this project. 3:24
We have a srcc folder or source folder. 3:27
A test folder, with a simple test for this application. 3:31
And we can use npm's built in test task for this. 3:36
Let's jump in quickly to the package.json file and see what's going on in there. 3:40
We'll be using Mocha as our test library and 3:47
UglifyJs to combine our JavaScript files into one app.js file. 3:50
The scripts object has a test task. 3:56
If you don't specify your test, it prints out, Error: no test specified, and 4:00
it exits with a 1, meaning something went wrong. 4:05
Computer programs successfully run exits with the code of 0. 4:08
The thing that's wrong here, is that we have no test set up. 4:14
We'll do that in the next video. 4:18
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up