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 ERR! code E401 npm ERR! 401 Unauthorized - Token has expired
Token has expired - npm authentication failure
npm ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code EAI_NODATA npm ERR! errno EAI_NODATA npm ERR! getaddrinfo EAI_NODATA registry.npmjs.org
How to fix "npm ERR! code EAI_NODATA - getaddrinfo EAI_NODATA"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"