When trying to import 'fs' I get the error: "Module not found: Can't resolve 'fs'" #54176
-
Verify canary release
Provide environment informationOperating System:
Platform: linux
Arch: x64
Version: #1635-Microsoft Fri Jan 01 08:00:00 PST 2016
Binaries:
Node: 18.15.0
npm: 9.5.0
Yarn: 1.22.19
pnpm: N/A
Relevant Packages:
next: 13.4.13
eslint-config-next: 13.4.13
react: 18.2.0
react-dom: 18.2.0
typescript: 5.1.6
Next.js Config:
output: N/A Which area(s) of Next.js are affected? (leave empty if unsure)No response Link to the code that reproduces this issue or a replay of the bughttps://github.com/ArturKazuo/painel-test To ReproduceBy running "npm run dev" or "npm run build" you will get the following error message: "Module not found: Can't resolve 'fs'" Describe the BugI am building a front end page with nextjs. To enable some features, such as clicking on buttons, I need to enable "use client" on the page. However, in doing this, I am no longer able to import 'fs'. Does anyone have any ideia how to fix this? Expected BehaviorBy running "npm run dev" or "npm run build" you will get the following error message: "Module not found: Can't resolve 'fs'" Which browser are you using? (if relevant)No response How are you deploying your application? (if relevant)No response |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 13 replies
-
Hi, When you use the When you use the There's a problem when combining those, because You have to refactor your code. So that the module that uses It was often the case that developers kept everything into one file, creating 1000+ lines files that mixed imports and relied on bundlers to be smart enough to figure out what to discard. With RSC that's a bit more nuanced. Because 'use client' modules are shipped to the browser, to be used to do hydration and rendering of client components. This has the added benefit that everything that is not under a What you need to do is "move to the leaves", that is refactor so that the component uses interactivity is a child of the page. |
Beta Was this translation helpful? Give feedback.
-
I get it now, thank you! I have another problem now, though. In my situation, I would need to use a package that uses 'fs' as a dependency, hence why I tested if 'fs' could be imported directly. Is there any way to listen to the onClick event in the client component and somehow alert the server side component that the button has been clicked, so that I can use the package (@wppconnect-team/wppconnect) ? |
Beta Was this translation helpful? Give feedback.
-
This is the discussion I was looking for. I am facing the same issue and reached the same conclusion mentioned here, to use Route Handlers to use However, this doesn't fix the problem in my case. I still get the error: 'Can't resolve fs'. I'm not sure, but maybe it's because I'm trying to fetch data from a client component? I'm attempting to integrate OpenAI's API for text-to-speech into a Next.js app, but I can't get |
Beta Was this translation helpful? Give feedback.
-
This worked for me: Subscribe here if it worked for you too, it helps me. |
Beta Was this translation helpful? Give feedback.
-
It's common to encounter the error "Module not found: Can't resolve 'fs'" when attempting to import the 'fs' module in a client-side context, such as within a component using ' For those who may encounter this issue in the future, it's important to remember that 'fs' can only be used on the server-side. If you need to perform file system operations within a client-side context, consider alternative approaches using browser APIs like the File API or delegate these operations to the server-side using API routes or server-side scripts. |
Beta Was this translation helpful? Give feedback.
-
thank you it worked for me when I change fs version to |
Beta Was this translation helpful? Give feedback.
-
👍
…On Mon, 23 Sept, 2024, 12:34 pm MrBeanDev, ***@***.***> wrote:
thank you it worked for me when I change fs version to "fs": "^0.0.2" ✅
from "fs": "^0.0.1-security" ❌.
—
Reply to this email directly, view it on GitHub
<#54176 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AVLFJM5S6MGA4JHO6YCGNGTZX64RFAVCNFSM6AAAAAA3UIPBF6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANZSGI2TENY>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
There are more than one solution because there are also more than one problem which can cause this message. So, I researched and found 6 solutions about that and shared on my dev.to post Module not found: Can't resolve 'fs' because it is too long. I share one of them below: 1. Ensure
|
Beta Was this translation helpful? Give feedback.
Hi,
When you use the
fs
module, you are banking on that API being present on the environment where your code runs.When you use the
use client
directive, you are telling the framework to bundle the code for the component to be used in the browser.There's a problem when combining those, because
fs
doesn't exist in the browser.You have to refactor your code. So that the module that uses
fs
is not mixed with the module that isuse client
.It was often the case that developers kept everything into one file, creating 1000+ lines files that mixed imports and relied on bundlers to be smart enough to figure out what to discard.
With RSC that's a bit more nuanced. Because 'use client' modules ar…