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.
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.
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
vitestWhen 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.
npm error code ENOENT npm error syscall spawn git npm error path git npm error errno -4058 npm error enoent An unknown git error occurred
How to fix "spawn git ENOENT" in npm
npm error code E401 npm error Incorrect or missing password.
How to fix 'E401 Unable to authenticate' errors with npm private registries
npm notice access token expired or revoked. Please try logging in again.
Token has expired - npm authentication failure
npm ERR! code EAI_AGAIN
How to fix "EAI_AGAIN" in npm
npm error code E403 npm error 403 Forbidden - PUT https://registry.npmjs.org/<package>
How to fix 'E403 Forbidden' error in npm