Debugging Next.js: How to fix the `can't resolve module` error in Next.js

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


I ran into this issue with Next.js:

Module not found: Can’t resolve ‘fs’

In a Next.js page your can import methods from a file that loads Node.js modules.

This is fine, as long as you also use the imported method in getStaticProps().

Example, I had this code:

import { getData } from '../lib/data'

//...

export async function getStaticProps() {
  const data = getData()
  return {
    props: {
      data,
    },
  }
}

When I commented const data = getData(), Next started giving me the error 'fs' module not found because fs was the first module I imported in lib/data.

It might happen with any other Node library you import first.

This happens because anything in getStaticProps() is just called when ran in a server environment, but if we don’t invoke the Node.js function in there, Next.js can’t know that.

Lessons in this unit:

0: Introduction
1: Blank page after router.push() in Next.js?
2: How to fix the error `PrismaClient is unable to be run in the browser` in Next.js
3: Next.js, blank page after calling `res.redirect()`
4: Next.js, how to fix the error `Constructor requires 'new' operator`
5: Next.js, fix the `module not found` error
6: ▶︎ How to fix the `can't resolve module` error in Next.js
7: How to fix error serializing Date object JSON in Next.js
8: How to fix the `unable to resolve dependency tree` PostCSS and Tailwind issue in Next.js
9: Fix “Module not found: Can't resolve encoding” in Next.js
10: How to fix Your custom PostCSS configuration must export a `plugins` key.
11: Next.js, what to do when the state of a component is not refreshed when navigating
12: Revalidation and ISR gotcha on Vercel
13: How to fix the `Already 10 Prisma Clients are actively running` error