Back to Tutorials
Node.js
16 min read
Sahasransu Satpathy
9/1/2025

Common Node.js / Express Errors & Solutions

Fix common Node.js & Express errors with clear explanations, examples, and solutions.

<h2>Introduction</h2>
<p>Node.js and Express are powerful tools for backend development, but developers often face tricky errors. This guide covers the <strong>most common Node.js / Express errors</strong> and their solutions.</p>

<h3>1. Port Already in Use (EADDRINUSE)</h3>
<p>Error: <code>Error: listen EADDRINUSE: address already in use :::5000</code></p>
<h4>✅ Solution</h4>
<pre><code>// Kill the process using the port

npx kill-port 5000

// Or change port const PORT = process.env.PORT || 5001;</code></pre>

<h3>2. Cannot Find Module</h3>
<p>Error: <code>Error: Cannot find module 'express'</code></p>
<h4>✅ Solution</h4>
<pre><code>npm install express

// or check your import const express = require('express');</code></pre>

<h3>3. Unexpected Token 'import'</h3>
<p>This happens when using ES Modules without enabling them.</p>
<h4>✅ Solution</h4>
<pre><code>// package.json

{ "type": "module" }</code></pre> <p>Or use <code>require()</code> instead of <code>import</code>.</p>

<h3>4. req.body Undefined</h3>
<p>In Express, <code>req.body</code> is undefined without middleware.</p>
<h4>✅ Solution</h4>
<pre><code>const express = require('express');

const app = express();

app.use(express.json()); app.use(express.urlencoded({ extended: true }));</code></pre>

<h3>5. CORS Error</h3>
<p>Frontend cannot access backend API.</p>
<h4>✅ Solution</h4>
<pre><code>const cors = require('cors');

app.use(cors());</code></pre>

<h3>6. MongoDB Connection Error</h3>
<p>Error connecting to MongoDB with Mongoose.</p>
<h4>✅ Solution</h4>
<pre><code>mongoose.connect(process.env.MONGO_URI, {

useNewUrlParser: true, useUnifiedTopology: true, }) .then(() => console.log('MongoDB Connected')) .catch(err => console.error(err));</code></pre>

<h3>7. Nodemon Not Restarting</h3>
<p>Nodemon does not detect changes.</p>
<h4>✅ Solution</h4>
<pre><code>npm install -g nodemon

nodemon index.js</code></pre> <p>Or add to <code>package.json</code>:</p> <pre><code>"scripts": { "dev": "nodemon index.js" }</code></pre>

<h3>8. Asynchronous Errors Not Caught</h3>
<p>Errors inside async functions crash the app.</p>
<h4>✅ Solution</h4>
<pre><code>app.get('/', async (req, res, next) => {

try { const data = await fetchData(); res.json(data); } catch (err) { next(err); } });</code></pre>

<h3>9. res.send Called Multiple Times</h3>
<p>Causes <code>ERR_HTTP_HEADERS_SENT</code>.</p>
<h4>✅ Solution</h4>
<pre><code>if (!user) return res.status(404).send('User not found');

res.send(user);</code></pre>

<h3>10. Environment Variables Not Working</h3>
<p>Error: <code>process.env.VAR_NAME is undefined</code></p>
<h4>✅ Solution</h4>
<pre><code>npm install dotenv

// index.js require('dotenv').config(); const PORT = process.env.PORT;</code></pre>

<h3>Best Practices</h3>
<ul>
  <li>Always handle async errors with try/catch or middleware.</li>
  <li>Use <code>dotenv</code> for configuration.</li>
  <li>Keep routes, controllers, and configs organized.</li>
  <li>Log errors clearly for debugging.</li>
</ul>

<h3>Conclusion</h3>
<p>By understanding these common Node.js/Express errors and solutions, you can save hours of debugging and write more reliable applications.</p>

Previous Tutorial

Browse All Tutorials