What is the npm init test command?

Posted on Jun 27, 2022

Learn what is the test command generated by npm init in your package.json file

Photo from Unsplash

The npm init command is used to generate a package.json file for your JavaScript project.

The command will generate a series of prompts for you to fill. Your answers will be the value of the properties in the JSON file.

Once the file is generated, you’ll see the entries as shown below:

{
  "name": "n-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

When you leave the test command blank, npm will still add the test script as highlighted above.

The test command is an arbitrary command used as a convention to run the testing process. It will always be generated when you run the npm init command.

If you don’t change the value of the script, then it will call the echo command to print a message and then immediately exits the process:

# 👇 exampe output of npm test default command
$ npm test

> n-app@1.0.0 test
> echo "Error: no test specified" && exit 1

Error: no test specified

You can ignore this test command, or you can change it to any command that’s useful for your project.

For example, you can make the test command runs a test.js script:

{
  "scripts": {
    "test": "node test.js"
  }
}

You can also run a test runner like mocha or jest with the test command.

When you don’t need the test command, then you can remove it from the package.json file.

And that will be all about the test command generated by npm init.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.