How to fix npm ERR! missing script: 'build' in Node.js
This error occurs when you try to run an npm script that is not defined in the scripts section of your package.json file. npm cannot execute a script it cannot find.
Quick fix
Fixes npm ERR! missing script: 'build'
npm run- Verify available scripts
- Check you are in the correct directory
- Add the missing script to package.json
- Check for duplicate scripts sections
npm runnpm ERR! missing script: 'build'
What this error means
This error appears when you attempt to run an npm script using `npm run <script-name>` but that script is not defined in the "scripts" object inside your package.json file. npm looks for script definitions in this specific location, and when it cannot find the requested script name, it throws this error. The error message shows the exact script name that npm was looking for but could not find. This is npm's way of telling you that either the script was never defined, was misspelled, or exists in a different location than where you're running the command.
Typical symptoms
- npm run command fails immediately with "missing script" error
- Error shows the exact script name that could not be found
- Running npm run without arguments shows the script is not listed
- CI/CD pipelines fail at the build or deployment step
- Documentation or tutorials reference scripts that do not exist in your package.json
Common causes
- The script is not defined in the "scripts" section of package.json
- Running the npm command from the wrong directory (not the project root)
- Typo in the script name when running npm run command
- Multiple "scripts" objects in package.json (second one overrides the first)
- Following outdated documentation that references non-existent scripts
- Cloned repository missing script definitions added by the original author
- Corrupted or malformed package.json file preventing proper parsing
How to fix "npm ERR! missing script: 'build'"
Run npm run without any arguments to see all currently defined scripts:
npm runThis will display a complete list of available scripts. Check if the script you're trying to run is listed. If it's not there, you need to add it.
Ensure you're running the command from your project's root directory where package.json is located:
# Verify package.json exists
ls package.json
# Or view its contents
cat package.jsonIf package.json is not in the current directory, navigate to the correct location using cd.
Open package.json and add the script to the "scripts" object. For example, to add a build script:
{
"name": "your-project",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"build": "webpack --mode production",
"test": "jest"
}
}Common script definitions:
- build: "webpack" or "react-scripts build" or "tsc"
- dev: "nodemon index.js" or "next dev"
- start: "node index.js" or "next start"
- test: "jest" or "mocha"
The exact command depends on your project's build tool and framework.
Search your package.json for multiple "scripts" objects. If you find more than one, merge them into a single section:
Incorrect (duplicate scripts):
{
"scripts": {
"start": "node index.js"
},
"dependencies": {...},
"scripts": {
"build": "webpack"
}
}Correct (merged):
{
"scripts": {
"start": "node index.js",
"build": "webpack"
},
"dependencies": {...}
}JSON objects cannot have duplicate keys; the second one overwrites the first.
Check that your package.json is valid JSON with no syntax errors:
# Use Node.js to validate
node -e "console.log(JSON.parse(require('fs').readFileSync('package.json', 'utf8')))"Or use an online JSON validator. Common issues include:
- Missing commas between properties
- Trailing commas after the last property
- Unescaped quotes in string values
- Missing closing braces or brackets
If the file is corrupted, restore it from version control or regenerate it.
If you've made changes to package.json or suspect corruption, reinstall dependencies:
# Remove existing installations
rm -rf node_modules package-lock.json
# Reinstall
npm installThis ensures npm has the latest configuration and all dependencies are properly installed.
Advanced notes
Script Naming Conventions: While npm allows any script name, certain names have special behavior. Scripts like "preinstall", "postinstall", "prestart", "poststart", etc. are lifecycle scripts that run automatically before or after their corresponding commands. Avoid accidentally using these names unless you intend this behavior.
Workspace Considerations: In npm workspaces (monorepos), you may need to run scripts from specific packages using npm run <script> --workspace=<package-name> or navigate to the workspace directory. Scripts defined at the root level are separate from scripts in individual workspaces.
Script Inheritance: Some build tools and frameworks provide default scripts even if not explicitly defined in package.json. For example, create-react-app projects have default "start", "build", and "test" scripts provided by react-scripts. If you're using such a tool, ensure the dependency is installed correctly.
Environment-Specific Scripts: Consider using different script names for different environments (e.g., "build:prod", "build:dev", "start:local") to avoid confusion and make your build process more explicit.
Debugging Scripts: Use the --verbose flag to see detailed output when running scripts: npm run build --verbose. This can help identify issues with the script command itself versus the script definition.
Frequently asked questions
- What does "npm ERR! missing script: 'build'" mean?
- json file. npm looks for script definitions in this specific location, and when it cannot find the requested script name, it throws this error. The error message shows the exact script name that npm was looking for but could not find.
- How do I fix "npm ERR! missing script: 'build'"?
- This error occurs when you try to run an npm script that is not defined in the scripts section of your package.json file. npm cannot execute a script it cannot find.
- What causes "npm ERR! missing script: 'build'"?
- The script is not defined in the "scripts" section of package.json Running the npm command from the wrong directory (not the project root) Typo in the script name when running npm run command
- Is the "npm ERR! missing script: 'build'" error serious?
- It is a low-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Node.js Official Documentation
- ๐npm run-script Documentation(Official Docs)
- ๐npm ERR! missing script Issue(GitHub)
- ๐