How to fix SyntaxError: Unexpected token < in JSON at position 0 (express.json() failed) in Node.js

Node.jsBEGINNERHIGHUpdated Dec 9, 2025· 169 views

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.

Quick fix

Fixes SYNTAX_ERROR

// 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');
  1. Verify the API endpoint URL is correct
  2. Log the response before parsing as JSON
  3. Ensure your Express route is properly defined
  4. Verify Content-Type headers
node.js — error
// 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');
SyntaxError: Unexpected token < in JSON at position 0 (express.json() failed)

What this error means

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.

Typical symptoms

  • POST/PUT requests to your API endpoints fail with SyntaxError
  • Error occurs in express.json() middleware before reaching your route handlers
  • Works in Postman/cURL but fails in your client-side code
  • Error messages show the position 0, meaning the error is at the very beginning of the response
  • Server returns HTML error page instead of JSON response

Common causes

  1. Requesting a non-existent API route that returns an HTML 404 error page instead of JSON
  2. Client code sending malformed or invalid JSON in the request body
  3. Server not returning correct Content-Type: application/json header
  4. API endpoint URL is incorrect or pointing to wrong server
  5. Middleware ordering issue - express.json() placed after other middleware that consumes the request body
  6. Server code hasn't been restarted after adding a new route, so old behavior is still active
  7. Proxy or load balancer returning HTML error pages for failed backends

How to fix "SyntaxError: Unexpected token < in JSON at position 0 (express.json() failed)"

1Verify the API endpoint URL is correct

Check that your client is sending requests to the correct API endpoint. Common issues:

javascript
// 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.

2Log the response before parsing as JSON

Before calling .json(), log the response text to see what you're actually getting:

javascript
// 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.

3Ensure your Express route is properly defined

Make sure the route handler exists and is defined BEFORE express.json() is called:

javascript
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.

4Verify Content-Type headers

Ensure your client is sending the correct Content-Type header when making requests:

javascript
// 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.

5Validate JSON before sending

Ensure your client is sending valid JSON:

javascript
// 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');
});
6Restart your Node.js server

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:

bash
# Stop the server
Ctrl+C

# Restart
node server.js
# or with npm scripts
npm start

For development, use a tool like nodemon that automatically restarts on file changes.

Advanced notes

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.

Frequently asked questions

What does "SYNTAX_ERROR" mean?
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. parse() internally, which throws this error when it encounters unexpected characters at the start of the input stream.
How do I fix "SYNTAX_ERROR"?
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.
What causes "SYNTAX_ERROR"?
Requesting a non-existent API route that returns an HTML 404 error page instead of JSON Client code sending malformed or invalid JSON in the request body Server not returning correct Content-Type: application/json header
Is the "SYNTAX_ERROR" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading