Solve - Missing script: "test" npm error

avatar

Borislav Hadzhiev

Mon Mar 28 20222 min read

Solve - Missing script: "test" npm error #

The Missing script: "test" error occurs for multiple reasons:

  1. Missing a test script in the scripts section of your package.json file.
  2. Opening your IDE or shell in a directory that doesn't contain your package.json file.
  3. Not having initialized a package.json file for your project.
  4. Having multiple scripts objects in your package.json file.

npm missing script test

To solve the Missing script: "test" error, make sure to add a test command to the scripts object in your package.json file and open your shell or IDE in the root directory of your project before running the npm run test command.

First, make sure your package.json file has a test command in the scripts object.

package.json
{ "scripts": { "test": "jest" } }

The test command in your package.json file depends on your setup.

package-json-scripts-section

If you have a test command in the scripts object in your package.json file, make sure you are opening your IDE and shell in the root directory of your project.

You should open your IDE or shell in the directory where your package.json file is to be able to run the npm run test command.

If you try to run the command from a different directory, it won't find your package.json file and the Missing script: "test" npm error is caused.

If you don't have a package.json file, you have to create one by opening your terminal in your project's root directory and running the command npm init -y.

shell
npm init -y

Make sure to install all of the packages you use in your project before running the test command.

Now you are able to add a test command in the scripts object of your package.json file.

package.json
{ "scripts": { "test": "jest" } }
Another cause of the error is having multiple scripts objects in your package.json file.

This causes the error because the second scripts object overrides the first, and your test command might only be present in your first scripts object.

If you don't want to add a test command to the scripts in your package.json file, you can run the command directly from your shell (assuming you have the specific package installed globally).

shell
jest

The examples in this article assume you use the jest package as your test runner.

If you haven't installed or configured jest, refer to the Getting Started section in their Github repository.

Use the search field on my Home Page to filter through my more than 1,000 articles.