next.js: Module not found: Can't resolve 'fs'
Bug report
Describe the bug
I’m using the popular find-up npm package, which has locate-path as a dependency. locate-path requires fs within it’s code.
When I attempt to run my app I get the following error message:
[ error ] ./node_modules/locate-path/index.js
Module not found: Can't resolve 'fs' in 'C:\...\node_modules\locate-path'
Could not find files for /index in .next/build-manifest.json
Promise { <pending> }
ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in 'C:\...\node_modules\locate-path'
at factory.create (C:\...\node_modules\webpack\lib\Compilation.js:823:10)
at factory (C:\...\node_modules\webpack\lib\NormalModuleFactory.js:397:22)
at resolver (C:\...\node_modules\webpack\lib\NormalModuleFactory.js:130:21)
at asyncLib.parallel (C:\...\node_modules\webpack\lib\NormalModuleFactory.js:224:22)
at C:\...\node_modules\neo-async\async.js:2830:7
at C:\...\node_modules\neo-async\async.js:6877:13
at normalResolver.resolve (C:\...\node_modules\webpack\lib\NormalModuleFactory.js:214:25)
at doResolve (C:\...\node_modules\enhanced-resolve\lib\Resolver.js:184:12)
at hook.callAsync (C:\...\node_modules\enhanced-resolve\lib\Resolver.js:238:5)
at _fn0 (eval at create (C:\...\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
at resolver.doResolve (C:\...\node_modules\enhanced-resolve\lib\UnsafeCachePlugin.js:37:5)
at hook.callAsync (C:\...\node_modules\enhanced-resolve\lib\Resolver.js:238:5)
at _fn0 (eval at create (C:\...\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
at hook.callAsync (C:\...\node_modules\enhanced-resolve\lib\Resolver.js:238:5)
at _fn0 (eval at create (C:\...\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:27:1)
at resolver.doResolve (C:\...\node_modules\enhanced-resolve\lib\DescriptionFilePlugin.js:42:38)
I created an issue in the locate-path repo and they have confirmed the issue is not with them, but likely with webpack. I don’t use webpack in my app so the issue must be arising from Next.js’s use of webpack.
To Reproduce
Clone https://github.com/TidyIQ/nextjs-issue and run npm run dev.
Expected behavior
No issue
System information
- OS: Windows 10
- Version of Next.js: 8.1.1-canary.67
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 64
- Comments: 32 (5 by maintainers)
Commits related to this issue
- fixes: webpack vs 'cant resolve fs' https://github.com/vercel/next.js/issues/7755#issuecomment-508633125 — committed to abendy/nextjs-redux-starter by abendy 6 years ago
- :bug: Fix animation export webm-writer was broken because of an issue with fs, this solved it somehow https://github.com/vercel/next.js/issues/7755#issuecomment-508633125 — committed to pouretrebelle/jigsaws by pouretrebelle 6 years ago
- :bug: Fix animation export webm-writer was broken because of an issue with fs, this solved it somehow https://github.com/vercel/next.js/issues/7755#issuecomment-508633125 — committed to pouretrebelle/jigsaws by pouretrebelle 6 years ago
Update for modern Next.js (9.4+)
You can safely use
fswithingetStaticPropsorgetServerSideProps, no extra configuration required. Be sure you’re referencing the variable in your data lifecycle so it’s correctly tree shaken away.You can use this tool to learn visually how it works!
If you’re still building on a legacy Next.js version with
getInitialProps, read below 👇The provided code is not valid – this file would never be available on the client side during rendering:
https://github.com/TidyIQ/nextjs-issue/blob/aef67b12d91d299d0978550005a40cbb34f74b71/pages/index.js#L5
Remember, you can only do FS-related operations while on the server. This means you cannot use
fswhile rendering.If you use
fs, be sure it’s only withingetInitialProps.You may also need to create a
next.config.jsfile with the following content to get the client bundle to build:For those trying
webpack 5on next.js, the fix is different:Reference: https://webpack.js.org/migrate/5/#clean-up-configuration
I found out what the problem was. If I import a function that uses
fs, but do not run/use the function inside getStaticProps, it will end up being included in the browser bundle. Once the function gets referenced inside getStaticProps it will stop appearing in the browser bundle.I guess there is some hidden logic that removes imports used in getStaticProps but unused in main export. I spent a few hours debugging until I could reproduce, maybe worth having a sidenote somewhere in the docs 😃
I had this same issue (using fs/promises) and applying any of the proposed solutions works:
Solution 1: Avoid uncalled functions on the module using fs/promises that was used from
getStaticProperties()Solution2 Adding this to your next.config.js
But these are workarounds, right? I mean, it’s difficult to maintain, as it’s difficult to detect that having functions on those modules that are not being called anymore is the cause of your problem.
I understand that tree shaking is tricky but, are there any plans to permanently fix this?
No, webpack tree shaking is not sophisticated enough to shake these exports in that way. Tree shaking getStaticProps / getServerSideProps / getStaticPaths is handled by this custom Babel plugin we created: https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts
When I add the proposed solution for webpack 5 the error disappears, but subsequent errors arise:
Module not found: Can't resolve 'http2'and when I install that the next one appearsModule not found: Can't resolve 'tls'and when I install that the next one appears… etc.Does anyone have any more/other suggestions? I’m running “next”: “^11.1.2”,
I had same issue, this comment helped https://github.com/vercel/next.js/issues/7755#issuecomment-508633125 But it took a while to find the culprit. Would be nice to have more informative error message, since it looks like a common mistake.
@AIEAJN you’re probably exporting the
pgmodule orfson the client-side. try pasting your relevant code in https://next-code-elimination.vercel.app/ to see what gets shipped to client.@ig-perez As of Next v9.4, they have a built-in way to load environment variables → https://nextjs.org/docs/basic-features/environment-variables
I suggest you read the docs to find the solution 😃
I had this error while using
fast-glob.I used this great tool to understand the code that gets bundled in the client-side.
Turned out, I was importing a variable from a file that uses
fast-globwhich internally usesfsbut I wasn’t using the variable anywhere insidegetStaticPropsso the files importsfast-globweren’t getting eliminated.An example:
mdxUtils.js
index.js
As you can see I am not using
BLOG_PATHanywhere inindex.jsbut still importing it. I am only usingblogFilePathsso it gave me this error.For more context → https://github.com/vercel/next.js/discussions/17138
Keep in mind Next’ code elimination only works under
/pages. If you have, say, a genericgetServerSidePropselsewhere, it might not work as expected.@tettoffensive Unfortunately for me the rabbit hole went deeper than 3 packages. I installed like 10 of them then gave up. The trace leads back to the
flamelinkmodule I’m using. The code runs server side so it should have access to fs (as it runs in a node environment). It’s as if webpack doesn’t understand that it has access to fs there.Edit:
My problem turned out to be a stupid mistake on my end: I had this in my code:
But getInitialProps runs on the client side so I had to replace it with:
Try this:
How do I tell next.js to use webpack 5. I still get this message
info - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5when I update this section to the above.I had the same issue. The problem turned out to be not referencing the imported variable within
getStaticPropsfunction. Thanks to @Timer for correctly pointing out.Hi all. Like @aloukissas I have the same issue when using
dotenvwithin myindex.jsgetStaticProps(). It gets solved when adding thenext.config.jsfile as shown in @Timer initial comment.Any clues why is this happening? I’m unsing
Next.jsv.9.4.0Thanks!