Run the basic npm scripts

Elijah Manor
InstructorElijah Manor
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

In this lesson we will examine two common scripts (start and test) that npm natively understands. We’ll define these scripts for our existing repository and show how you can run the scripts from the terminal.

Instructor: [00:00] Let's replace the test command that npm generated for us. We will say, "mocha." We'll provide the spec folder. Then we'll require babel-register, which will automatically compile our tests, since we're using Babel for ES2015 and beyond.

[00:18] We could open up our terminal and type "npm test" or just "npm t" to run our tests. We can see that all our tests passed.

[00:27] Let's go back and add a start script, which is another script that npm recognizes natively. For our start, we'll have it kick off the main entry point of our app, which is index.js.

[00:39] If we go back to our terminal, since this is a script that npm understands, we can just say, "npm start." Our program will execute as expected. If we look at our files, we'll see that, sure enough, the data.json was generated. We can inspect that just to make sure.

Nitish Dayal
Nitish Dayal
~ 8 years ago

For the sake of clarity, it might be worth it to specify that users should install mocha as a developer dependency (or global install or however you want to present it) in order for this to work.

Elijah Manor
Elijah Manorinstructor
~ 8 years ago

Good point Nitish, I tried to be pretty aware of 3rd party dev dependencies before showing them throughout the course, but that is one that I overlooked.

Rick
Rick
~ 8 years ago

As a complete newbie to npm, it seems that there are some missing things, besides mocha. My package.json doesn't have a bin object. When I run npm start I got an error saying I had no index.js. Fair enough. I created an empty index.js and the start command completed but did not generate a data.json. I'm missing something.

Markdown supported.
Become a member to join the discussionEnroll Today