To fix npm ERR! missing script: start (or npm error Missing script: "start" in npm v10+), inspect package.json in your working directory to verify whether a "start" entry exists under "scripts", and either add a "start" script (for example, "start": "node index.js" or "start": "node server.js" or your framework’s entry command) or run the exact script defined in package.json (such as npm run dev or npm run serve). Always guard against running npm start inside the wrong folder, verify the entry file exists on disk, and avoid mutating unrelated configuration or lockfiles unnecessarily.
1. Inspecting package.json and Adding a Safe "start" Script
Check the active directory and package.json contents first, confirm your actual entry script exists, and update "scripts" cleanly using a non-destructive read-modify-write helper in Node.js (or edit package.json directly):
// Inspect package.json scripts and safely add a "start" script if missing
const fs = require("fs");
const path = require("path");
function ensureStartScript(projectDir, startCommand = "node index.js") {
if (typeof projectDir !== "string" || projectDir.trim() === "") {
throw new TypeError("Expected projectDir to be a non-empty directory path.");
}
if (typeof startCommand !== "string" || startCommand.trim() === "") {
throw new TypeError("Expected startCommand to be a non-empty command string.");
}
const pkgPath = path.resolve(projectDir.trim(), "package.json");
if (!fs.existsSync(pkgPath)) {
throw new Error(`No package.json found at "${pkgPath}".`);
}
let parsed;
try {
parsed = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
} catch (err) {
throw new Error(`Invalid JSON in "${pkgPath}": ${err.message}`);
}
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
throw new TypeError("package.json root must be a JSON object.");
}
const existingScripts =
parsed.scripts && typeof parsed.scripts === "object" && !Array.isArray(parsed.scripts)
? { ...parsed.scripts }
: {};
if (Object.prototype.hasOwnProperty.call(existingScripts, "start")) {
return { updated: false, scripts: Object.freeze(existingScripts) };
}
const nextPkg = {
...parsed,
scripts: {
...existingScripts,
start: startCommand.trim()
}
};
fs.writeFileSync(pkgPath, `${JSON.stringify(nextPkg, null, 2)}\n`, "utf8");
return { updated: true, scripts: Object.freeze(nextPkg.scripts) };
}
// Example usage:
// const result = ensureStartScript(process.cwd(), "node server.js");
// console.log(result);
- Verifies directory & JSON validity: Prevents blind writes when
package.jsonis missing or malformed. - Preserves existing scripts & keys: Spreads existing
scriptswithout clobbering"test","build", or"dev".
2. Running the Actual Script Name or Checking Your Entry File
Many projects define "dev", "serve", or "server" instead of "start", or place package.json inside a nested subdirectory (such as ./client or ./server):
// List available npm scripts from package.json so you can run the right command
const fs = require("fs");
const path = require("path");
function listAvailableScripts(projectDir = process.cwd()) {
if (typeof projectDir !== "string" || projectDir.trim() === "") {
throw new TypeError("Expected projectDir to be a non-empty path.");
}
const pkgPath = path.resolve(projectDir.trim(), "package.json");
if (!fs.existsSync(pkgPath)) {
throw new Error(`package.json not found in "${path.resolve(projectDir)}".`);
}
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
const scripts = pkg && typeof pkg.scripts === "object" && pkg.scripts !== null ? pkg.scripts : {};
return Object.freeze(
Object.entries(scripts).map(([name, command]) => Object.freeze({ name, command }))
);
}
// Example usage:
// console.log(listAvailableScripts(process.cwd()));
// Then run e.g. `npm run dev` if "dev" is listed
- Matches project conventions: CLI/bundler templates frequently use
npm run devduring development rather thannpm start. - Safe read-only inspection: Never mutates files on disk just to discover available scripts.
3. Common Traps to Avoid
Fixing "npm ERR! missing script: start" often runs into a few pitfalls:
- Running
npm startfrom a parent or sibling folder: npm looks forpackage.jsonin the current working directory—check your folder path (pwd/cd) before editing anything. - Adding
"start": "node index.js"when your entry file isserver.jsorsrc/app.js: Point"start"to the real entry file (and guard missing files/errors cleanly) sonpm startdoesn’t fail withMODULE_NOT_FOUND. - Running
npm startbefore initializing a project (npm init): If nopackage.jsonexists yet, initialize one (npm init -y) and define"scripts"rather than guessing.
TL;DR: Open package.json, check "scripts" in your current folder, and either add "start": "<your-entry-command>" (such as "node index.js" or "node server.js") or run the existing script (like npm run dev) with npm run <script-name>.
