Next.js, fix the `module not found` error
New Courses Coming Soon
Join the waiting lists
While doing some sanitization on a variable in Next.js I ran into this weird problem:
Module not found: Error: Can't resolve 'net'
You might have some variation of it, which says a core Node.js module is missing.
Do NOT npm install net
or anything like that. If you already tried, run npm uninstall
on those modules.
The problem is this: Next.js is trying to run backend code in the frontend.
In my case it was the DOMPurify library. I was using it inside the component, but instead I had to use it in the getStaticProps()
method.
That method runs at build time in the Node environment, where that library expected to be ran into.
→ Get my Next.js (pages router) Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- THE VALLEY OF CODE your web development manual
- BOOKS 17 coding ebooks you can download for free on JS Python C PHP and lots more
- Interesting links collection
- Follow me on X