How to fix npm ERR! Missing script: "test" in npm

npmBEGINNERLOWUpdated Dec 6, 2025ยท 207 views

The "Missing script: test" error occurs when running npm test but no test script is defined in package.json. Add a test script or use your test runner directly to resolve this.

Quick fix

Fixes npm ERR! Missing script: "test"

{
  "scripts": {
    "test": "jest"
  }
}
  1. Add a test script to package.json
  2. Add placeholder if no tests exist yet
  3. Verify you are in the correct directory
  4. Check for duplicate scripts objects
npm โ€” error
{
"scripts": {
"test": "jest"
}
}
npm ERR! Missing script: "test"

What this error means

This error means npm looked for a "test" script in your package.json file but couldn't find one. When you run `npm test` (or `npm run test`), npm searches the scripts section of package.json for a key named "test" and executes the associated command. This commonly happens with newly initialized projects where `npm init` creates a placeholder test script, or in projects where testing hasn't been set up yet. It's also frequent in CI/CD pipelines that include `npm test` as a default step.

Typical symptoms

  • npm test fails with "Missing script: test"
  • CI/CD pipelines (GitHub Actions, etc.) fail at test step
  • Git pre-commit hooks fail when they include npm test
  • Error shows list of available scripts but "test" is not among them
  • Works for some team members who have different package.json
  • Fresh project clone fails at test stage

Common causes

  1. No "test" key defined in package.json scripts section
  2. Running npm test from wrong directory without package.json
  3. Project initialized but test framework not configured
  4. Duplicate scripts objects in package.json (second overrides first)
  5. CI workflow includes npm test but project has no tests
  6. Placeholder "test" script was removed or commented out

How to fix "npm ERR! Missing script: "test""

1Add a test script to package.json

Open package.json and add a test script:

For Jest:

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

For Vitest:

json
{
  "scripts": {
    "test": "vitest"
  }
}

For Mocha:

json
{
  "scripts": {
    "test": "mocha"
  }
}
2Add placeholder if no tests exist yet

If your project doesn't have tests yet, add a placeholder:

json
{
  "scripts": {
    "test": "echo \"No tests specified\" && exit 0"
  }
}

This allows npm test to pass without failing. Replace with actual test command when ready.

3Verify you are in the correct directory

Ensure you're running npm from the project root:

bash
# Check current directory
pwd

# Verify package.json exists
ls package.json

# Check what scripts are defined
npm run

Navigate to the correct directory if needed.

4Check for duplicate scripts objects

Ensure package.json has only one scripts section:

Incorrect (second overrides first):

json
{
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  },
  "scripts": {
    "build": "webpack"
  }
}

Correct:

json
{
  "scripts": {
    "start": "node index.js",
    "test": "jest",
    "build": "webpack"
  }
}
5Update CI/CD workflow if tests not needed

If your project doesn't need tests, remove npm test from CI:

GitHub Actions example:

yaml
# Remove or comment out:
# - run: npm test

# Or make it conditional:
- run: npm test --if-present

The --if-present flag runs the script only if it exists.

6Run test framework directly

As a workaround, run your test framework directly:

bash
# Using npx (no script needed)
npx jest
npx vitest
npx mocha

# Or if globally installed
jest
vitest

Advanced notes

When using git hooks with Husky, the pre-commit hook often runs npm test. If you don't have tests, either add a placeholder script or modify .husky/pre-commit to remove the npm test command.

For projects that will never have tests (rare but possible), consider adding a test script that explicitly exits successfully to prevent CI/CD failures:

json
"test": "echo 'Tests not implemented' && exit 0"

In monorepos, each workspace package needs its own test script if the root runs tests across all packages.

Frequently asked questions

What does "npm ERR! Missing script: "test"" mean?
json file but couldn't find one. json for a key named "test" and executes the associated command. This commonly happens with newly initialized projects where npm init creates a placeholder test script, or in projects where testing hasn't been set up yet.
How do I fix "npm ERR! Missing script: "test""?
The "Missing script: test" error occurs when running npm test but no test script is defined in package.json. Add a test script or use your test runner directly to resolve this.
What causes "npm ERR! Missing script: "test""?
No "test" key defined in package.json scripts section Running npm test from wrong directory without package.json Project initialized but test framework not configured
Is the "npm ERR! Missing script: "test"" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading