DEV Community

Cover image for Common Next.js Errors (and How I Solved Them)
gary killen
gary killen

Posted on

Common Next.js Errors (and How I Solved Them)

Common Next.js Errors (and How I Solved Them)

If you've spent any amount of time building with Next.js, you've probably discovered that learning React is only half the battle. The other half is staring at an error message that makes absolutely no sense—until you finally figure it out hours later.

I've been building projects with Next.js over the past several months, and while the framework is incredible, I've hit my fair share of confusing errors. The good news? Every one of them taught me something valuable.

Here are some of the most common Next.js errors I ran into and how I eventually solved them.


1. ReferenceError: document is not defined

This was one of the first errors that completely confused me.

ReferenceError: document is not defined
Enter fullscreen mode Exit fullscreen mode

At first, I thought something was wrong with my code. The problem wasn't my code—it was where it was running.

Next.js renders pages on the server before sending them to the browser. Since the server doesn't have access to browser APIs like document or window, trying to use them immediately causes this error.

What caused it?

const width = window.innerWidth;
Enter fullscreen mode Exit fullscreen mode

This code executes during server-side rendering, where window doesn't exist.

How I fixed it

I moved the browser-specific code inside a useEffect() hook.

useEffect(() => {
  console.log(window.innerWidth);
}, []);
Enter fullscreen mode Exit fullscreen mode

Or, if necessary, I checked whether the code was running in the browser before accessing window.

💡 Lesson Learned

If you're using browser APIs, make sure your code only runs on the client.


2. Environment Variables Returning undefined

Few things are more frustrating than seeing this:

process.env.MONGODB_URI
Enter fullscreen mode Exit fullscreen mode

…and getting:

undefined
Enter fullscreen mode Exit fullscreen mode

In my case, I had simply forgotten to create or correctly configure my .env.local file.

Other times, I changed an environment variable but forgot to restart the development server.

My Debugging Checklist

  • Is the variable inside .env.local?
  • Did I restart npm run dev?
  • Does the variable need the NEXT_PUBLIC_ prefix?
  • Did I accidentally misspell the variable name?

💡 Lesson Learned

Environment variables are loaded when the development server starts—not while it's already running.


3. Hydration Errors

Hydration errors can look intimidating.

Hydration failed because the initial UI does not match what was rendered on the server.
Enter fullscreen mode Exit fullscreen mode

This usually means the HTML generated on the server doesn't match what React renders in the browser.

Common causes include:

  • Using Date.now()
  • Using Math.random()
  • Rendering different content on the client
  • Reading from localStorage during rendering

How I fixed it

Instead of rendering dynamic values immediately, I waited until the component mounted.

const [mounted, setMounted] = useState(false);

useEffect(() => {
  setMounted(true);
}, []);

if (!mounted) return null;
Enter fullscreen mode Exit fullscreen mode

💡 Lesson Learned

The first render should produce the same output on both the server and the client.


4. MongoDB Connection Problems

Database errors always seemed more complicated than they actually were.

Sometimes the error looked like this:

MongoServerError: authentication failed
Enter fullscreen mode Exit fullscreen mode

Other times:

querySrv ECONNREFUSED
Enter fullscreen mode Exit fullscreen mode

Or:

MONGODB_URI is undefined
Enter fullscreen mode Exit fullscreen mode

The fixes were usually simple:

  • Double-check the connection string.
  • Verify the username and password.
  • Confirm your IP address is allowed in MongoDB Atlas.
  • Make sure your internet connection isn't blocking DNS lookups.
  • Verify your environment variables.

💡 Lesson Learned

Database errors are often configuration problems rather than coding problems.


5. Module not found

Everyone has seen this one.

Module not found
Enter fullscreen mode Exit fullscreen mode

Sometimes I had:

  • Misspelled an import
  • Used the wrong file extension
  • Imported from the wrong folder
  • Forgotten to install a package

My Debugging Checklist

  • Does the file actually exist?
  • Is the import path correct?
  • Is the package installed?
  • Did I restart the development server?

💡 Lesson Learned

Most "module not found" errors are caused by simple oversights. Start with the basics before assuming something is seriously broken.


6. React Hook Errors

One of my favorites:

Invalid hook call
Enter fullscreen mode Exit fullscreen mode

Usually this happened because I accidentally placed a hook:

  • Inside an if statement
  • Inside a loop
  • Inside another function

React hooks must always be called in the same order.

❌ Incorrect

if (loggedIn) {
  useEffect(() => {});
}
Enter fullscreen mode Exit fullscreen mode

✅ Correct

useEffect(() => {
  if (loggedIn) {
    // do something
  }
}, [loggedIn]);
Enter fullscreen mode Exit fullscreen mode

💡 Lesson Learned

Always call hooks at the top level of your component.


7. Importing a Server Component into a Client Component

This one took me a while to understand.

With the App Router, Next.js separates components into:

  • Server Components
  • Client Components

If I needed state, event handlers, or browser APIs, I had to mark the component with:

'use client';
Enter fullscreen mode Exit fullscreen mode

Otherwise, Next.js assumed it was a Server Component.

💡 Lesson Learned

Not every component should be a Client Component. Add 'use client' only when you actually need client-side features.


8. Images Not Loading

I eventually discovered that Next.js doesn't automatically allow images from every domain.

The fix was adding the domain to next.config.js.

images: {
  remotePatterns: [
    {
      protocol: 'https',
      hostname: 'example.com',
    },
  ],
}
Enter fullscreen mode Exit fullscreen mode

💡 Lesson Learned

If you're using the next/image component with external images, make sure the image domain is configured.


Final Thoughts

When I first started with Next.js, every error felt like I had broken something beyond repair.

Over time, I realized that these errors weren't telling me I was a bad developer—they were teaching me how the framework actually works.

Understanding concepts like server-side rendering, client-side rendering, hydration, and environment variables made debugging much less intimidating.

The biggest improvement in my development skills didn't come from memorizing APIs.

It came from solving bugs.

Every confusing error forced me to understand another piece of how modern web applications work.

And honestly, that's where the real learning happens.


What About You?

What's the most frustrating Next.js error you've encountered?

Share it in the comments—I’d love to hear your debugging stories and how you solved them.

Top comments (0)