Configuring npm package.json scripts

Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 5 years ago

With a node package manager's (npm) package.json script property, you can preconfigure common tasks like running unit tests with npm $SCRIPT_NAME

Man 1: [00:01] When npm initialized our package.json file for us it created this scripts field and within it entered a test entry which right now is just going to echo out an error message. The scripts field is extremely useful and a recommended way for you to provide workflow scripts for your module for people that are using it. Right now, if we were just to say, npm test, it's going to spit out that error message that we see defined here. But making sure that your module's test can be run with npm test is a big part of being a good module author.

[00:43] To demonstrate this, I've just defined the functionality of our module to just be a simple function that will convert a string to upper case. I've written a simple mocha test here that requires that file. This ./ just means that it's going to look in the current directory and default to a file named "index." We just have a simple test here that just says expect up-hello to equal hello in uppercase. We can see that that does work. That test passes if we just run mochatest.js.

[01:20] We can see that one is passing. But in order for somebody using your module to know that you're using Mocha, that the file is called test.js, that's a little bit more work than they should really have to do. What it's better to do, is to go in here and update this test field with that exact command. If we put that into our test field we can then come down here and say, npm test, and it's going to run that exact command for us so we can see that our tests are passing.

[01:58] Now somebody can install your module, run npm test, and they don't need to know or care about what sort of testing library you're using or how things are set up. They know that your tests are passing. There's also a shortcut. You can do npm t. It's the same thing. Test and a few other ones are sort of special fields in the npm world. You can run them directly like we just saw with npm test. You can also add custom ones here.

[02:32] If we wanted to add something like a check version. We'll make that run Mocha version. If we then save this we can then run this custom script by doing npm run checkversion. Any time that you run a custom script you can just do it with npm run and the name. It will look for that name within your scripts field.

egghead
egghead
~ 32 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today