How to Resolve the Module not found: Can’t resolve 'fs' error in Next.js and WebPack

Learn to fix Module not found: Can’t resolve ‘fs’ on Next.js and Webpack

Picture of Nsikak Imoh, author of Macsika Blog
Abstract background with the text How to Resolve the Module not found: Can’t resolve 'fs' error in Next.js and WebPack
Abstract background with the text How to Resolve the Module not found: Can’t resolve 'fs' error in Next.js and WebPack

Table of Content

When you work with Next.js, you may run into the issue of

Module not found: Can't resolve 'fs'.

Next.js allows you to import methods from a file that loads Node.js modules in the pages component.

This is completely acceptable, however, you should also use the method you imported from Node.js in Next.js getStaticProps() or getServerSideProps().

Read more: Next.js Data Fetching: Everything to Know About CSR, SSR, SSG, and ISR

What is Module not found: Can't resolve 'fs' error in Next.js

The Module not found: Can't resolve 'fs' in Next.js error occurs when you import a Node.js module that is available on the server-side, but that is not available in the browser.

The file system module is a prime example of this.

When you call a server-side module, the dependencies, you need to make sure it is available at the browser as well.

Why do you get Module not found: Can't resolve 'fs' error in Next.js

You get the Module not found: Can't resolve 'fs' error in Next.js because Next.js runs everything in the getStaticProps().

Since Node.js modules are server-side, when you run your code in a server environment and do not invoke the Node.js function in there, Next.js can't know that.

For Example, take this code block:

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

//...

export async function getStaticProps() {
  return {
    props: {
      data,
    },
  }
}
Highlighted code sample.

This code will easily throw the Module not found: Can't resolve 'fs' error in Next.js because the imported function is not available at the browser.

How to Solve the Module not found: Can't resolve 'fs' error in Next.js and Webpack 4

To resolve the error with Webpack 4, you need to tell webpack to set the module to 'empty' on the client-side (!isServer).

This is also a solution when you are working with older Next.js versions.

The configuration essentially tells Webpack to create an empty module for fs, which effectively suppresses the error.

Update your Next.js config file (/next.config.js) with the following:

module.exports = {
    webpack: (config, { isServer }) => {
        if (!isServer) {
            // set 'fs' to an empty module on the client to prevent this error on build --> Error: Can't resolve 'fs'
            config.node = {
                fs: 'empty'
            }
        }

        return config;
    }
}
Highlighted code sample.

Read more: What is Next.js On-demand Incremental Static Regeneration and How to Use it?

How to Solve the Module not found: Can't resolve 'fs' error in Next.js and Webpack 5

To resolve the error with Webpack 5, you need to tell webpack not to resolve the module on the client-side (!isServer).

Update your Next.js config file (/next.config.js) with the following:

module.exports = {
    webpack: (config, { isServer }) => {
        if (!isServer) {
            // don't resolve 'fs' module on the client to prevent this error on build --> Error: Can't resolve 'fs'
            config.resolve.fallback = {
                fs: false
            }
        }

        return config;
    }
}
Highlighted code sample.

Note: for other modules such as path, you can add multiple arguments such as

{
  fs: false,
  path: false
}
Highlighted code sample.

Read more: Next.js CSR, SSR, SSG, and ISR: How to Select a Data Fetching Method

How to Solve the Module not found: Can't resolve 'fs' error in Next.js Page

To fix the error in Next.js, make the imported Node.js module available on the browser by calling it within the Next.js getStaticProps() or getServerSideProps().

In our code example above, add const data = getData(), to the code.

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

//...

export async function getStaticProps() {
  const data = getData();
  return {
    props: {
      data,
    },
  }
}
Highlighted code sample.

Wrap Off

The Module not found: Can't resolve 'fs' in Next.js error occurs when you import a Node.js module that is not available in the browser.

To resolve it, make the imported Node.js module available on the browser by calling it within the Next.js getStaticProps() or getServerSideProps().

You might get this error with any other Node library you import.

If you learned from this tutorial or it helped you in any way, please consider sharing and subscribing to our newsletter.

Connect with me.

Need an engineer on your team to grease an idea, build a great product, grow a business or just sip tea and share a laugh?