This error occurs when express.json() middleware tries to parse non-JSON content (typically HTML) as JSON. Most commonly caused by requesting a non-existent route that returns a 404 HTML page, or sending malformed JSON to your API.
This error happens when Node.js/Express attempts to parse HTTP request body content as JSON, but the content is invalid JSON format. The '<' character at position 0 typically indicates the content is HTML (like a 404 error page) rather than JSON. The express.json() middleware uses JSON.parse() internally, which throws this error when it encounters unexpected characters at the start of the input stream.
Check that your client is sending requests to the correct API endpoint. Common issues:
// WRONG - typo in URL or route doesn't exist
const response = await fetch('/api/usrs'); // should be /api/users
// CORRECT
const response = await fetch('/api/users');Use your browser DevTools (Network tab) to see exactly what URL is being called and what status code is returned. A 404 response means the route doesn't exist.
Before calling .json(), log the response text to see what you're actually getting:
// With fetch
const response = await fetch('/api/users');
const text = await response.text();
console.log('Response:', text);
const data = JSON.parse(text); // or response.json()
// With axios
const response = await axios.post('/api/users', data);
console.log('Response:', response.data);This will show you the exact content that caused the parse error. If it starts with '<', it's HTML.
Make sure the route handler exists and is defined BEFORE express.json() is called:
const express = require('express');
const app = express();
// Middleware BEFORE routes
app.use(express.json());
// Route handler AFTER middleware
app.post('/api/users', (req, res) => {
// req.body is now parsed JSON
console.log(req.body);
res.json({ success: true });
});
app.listen(3000);If a route doesn't match, Express will return a 404 HTML page by default.
Ensure your client is sending the correct Content-Type header when making requests:
// With fetch
const response = await fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: 'John' }),
});
// With axios
const response = await axios.post('/api/users', { name: 'John' }, {
headers: { 'Content-Type': 'application/json' },
});Also check that your server is returning responses with Content-Type: application/json.
Ensure your client is sending valid JSON:
// Check if data is valid JSON
const data = { name: 'John' };
try {
const json = JSON.stringify(data);
console.log('Valid JSON:', json);
} catch (e) {
console.error('Invalid JSON:', e);
}
// When receiving, wrap in try-catch
const response = await fetch('/api/users');
const data = await response.json().catch(err => {
console.error('Parse error:', err);
throw new Error('Failed to parse response as JSON');
});If you recently added or modified a route, the changes won't take effect until you restart the server. Stop the server (Ctrl+C) and start it again:
# Stop the server
Ctrl+C
# Restart
node server.js
# or with npm scripts
npm startFor development, use a tool like nodemon that automatically restarts on file changes.
This error can also occur when proxies, load balancers, or API gateways return HTML error pages for backend failures. Check your server logs to see what status code is being returned. If using TypeScript/Express, ensure all route handlers properly return JSON. For production debugging, add error handling middleware that logs the request and response details before the express.json() middleware attempts parsing.
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