How to fix npm ERR! Missing script: "test" in npm
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"
}
}- Add a test script to package.json
- Add placeholder if no tests exist yet
- Verify you are in the correct directory
- Check for duplicate scripts objects
{"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
- 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
- Duplicate scripts objects in package.json (second overrides first)
- CI workflow includes npm test but project has no tests
- Placeholder "test" script was removed or commented out
How to fix "npm ERR! Missing script: "test""
Open package.json and add a test script:
For Jest:
{
"scripts": {
"test": "jest"
}
}For Vitest:
{
"scripts": {
"test": "vitest"
}
}For Mocha:
{
"scripts": {
"test": "mocha"
}
}If your project doesn't have tests yet, add a placeholder:
{
"scripts": {
"test": "echo \"No tests specified\" && exit 0"
}
}This allows npm test to pass without failing. Replace with actual test command when ready.
Ensure you're running npm from the project root:
# Check current directory
pwd
# Verify package.json exists
ls package.json
# Check what scripts are defined
npm runNavigate to the correct directory if needed.
Ensure package.json has only one scripts section:
Incorrect (second overrides first):
{
"scripts": {
"start": "node index.js",
"test": "jest"
},
"scripts": {
"build": "webpack"
}
}Correct:
{
"scripts": {
"start": "node index.js",
"test": "jest",
"build": "webpack"
}
}If your project doesn't need tests, remove npm test from CI:
GitHub Actions example:
# Remove or comment out:
# - run: npm test
# Or make it conditional:
- run: npm test --if-presentThe --if-present flag runs the script only if it exists.
As a workaround, run your test framework directly:
# Using npx (no script needed)
npx jest
npx vitest
npx mocha
# Or if globally installed
jest
vitestAdvanced 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:
"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
- ๐npm Official Documentation
- ๐
- ๐ฌ
- ๐