This error occurs when Node.js tries to parse your package.json file but finds invalid JSON syntax. Common culprits include missing commas, mismatched quotes, or trailing commas. The error can be fixed by validating the JSON structure and correcting syntax errors.
When Node.js or npm starts, it reads and parses the package.json file to understand your project configuration. If the JSON syntax is invalid—such as missing commas between properties, unquoted keys, or extra closing braces—the JSON parser throws an error at the position where it encounters the problem. The position number (like 123) tells you roughly where in the file the syntax error occurs. This prevents npm from reading your dependencies and prevents your Node.js application from starting. The "Unexpected token" error means the parser found a character it didn't expect at that specific position in the file. Unlike JavaScript, JSON has strict syntax rules: all property names must be in double quotes, no trailing commas are allowed, and no comments are permitted.
Copy the contents of your package.json and paste it into a JSON validator like JSONLint (jsonlint.com) or the built-in validator in VS Code. The validator will pinpoint the exact line and character where the syntax error occurs.
Review your package.json for these common errors:
1. Missing commas: Each property except the last must be followed by a comma
{
"name": "my-app",
"version": "1.0.0"
}2. Trailing commas: Remove commas after the last property
{
"name": "my-app",
"version": "1.0.0" // No comma here
}3. Use double quotes: JSON requires double quotes, not single quotes
{
"name": "my-app" // Correct
'name': 'my-app' // Wrong
}Open package.json in VS Code, Sublime Text, or a similar editor. Most modern editors highlight JSON syntax errors in red. The error tooltip will help you identify the problem. Fix the syntax error and save the file.
If the file looks corrupted with strange symbols, the file may have been saved with incorrect encoding. Right-click the file > Properties (Windows) or Get Info (macOS), then ensure it's saved as UTF-8 without BOM. In VS Code, click the encoding indicator ("UTF-8") in the bottom right and select "Reopen with Encoding > UTF-8".
If you have the file in git, revert to the last known good version:
git checkout HEAD -- package.jsonOr restore from a backup if version control is not available.
Once package.json is corrected, clear the npm cache and reinstall dependencies:
npm cache clean --force
npm installFor large projects or automated validation, use the package-json-validator npm package to programmatically validate package.json structure. This ensures your package.json conforms to npm's schema before attempting to parse it. Additionally, if working with monorepos or complex dependency trees, consider using npm lint or third-party tools like publint to validate the entire structure. When managing package.json programmatically, always use JSON serialization libraries that produce valid JSON (e.g., JSON.stringify() in Node.js) rather than string concatenation to avoid syntax errors.
Error: EMFILE: too many open files, watch
EMFILE: fs.watch() limit exceeded
Error: Middleware next() called multiple times (next() invoked twice)
Express middleware next() called multiple times
Error: Worker failed to initialize (worker startup error)
Worker failed to initialize in Node.js
Error: EMFILE: too many open files, open 'file.txt'
EMFILE: too many open files
Error: cluster.fork() failed (cannot create child process)
cluster.fork() failed - Cannot create child process