-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module not found: Can't resolve 'fs' #7755
Comments
Update for modern Next.js (9.4+)You can safely use You can use this tool to learn visually how it works! If you're still building on a legacy Next.js version with The provided code is not valid -- this file would never be available on the client side during rendering: Remember, you can only do FS-related operations while on the server. This means you cannot use If you use You may also need to create a module.exports = {
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
if (!isServer) {
config.node = {
fs: 'empty'
}
}
return config
}
} |
I have the same problem on my local, practically vanilla install, however in the examples from nextjs this doesn not seem to be a problem what makes it work there without modifying the config file? |
Only use |
I found out what the problem was. If I import a function that uses 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 :) |
Isn't the hidden logic just webpack's tree shaking? Makes sense when you think of it, Next doesn't import If you don't reference it anywhere, I guess webpack considers you imported it for side effects, so it still includes it in every bundle. |
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 |
Oh thanks for the pointer, looks like I overestimated webpack :) |
Any reason this would be happening from within |
Hi all. Like @aloukissas I have the same issue when using Any clues why is this happening? I'm unsing Thanks! |
I had this error while using 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 An example: mdxUtils.jsimport glob from 'fast-glob'
import path from 'path'
export const BLOG_PATH = path.join(process.cwd(), 'posts')
export const blogFilePaths = glob.sync(`${BLOG_PATH}/blog/**/*.mdx`) index.jsimport { BLOG_PATH, blogFilePaths } from './mdxUtils'
export const getStaticProps = () => {
const posts = blogFilePaths.map((filePath) => {
...
}
return { props: { posts } }
} As you can see I am not using For more context → #17138 |
Thanks @deadcoder0904 this is my code: import Layout from '../components/template'
import Main from '../components/main'
import Menu from '../components/menu'
import 'dotenv/config'
export async function getStaticProps () {
const avatarLocation = process.env.AVATAR_URL
const avatarTitle = process.env.AVATAR_TITLE
return {
props: {
avatarLocation,
avatarTitle
}
}
}
export default function RenderMainPage ({ avatarLocation, avatarTitle }) {
return (
<Layout
avatarURL={avatarLocation}
topLeft={<Menu />}
middle={<Main avatarURL={avatarLocation} avatarTitle={avatarTitle} />}
/>
)
} The tool you mention shows Probably there's another better way to do it, I'm still learning Next.js :) |
@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 :) |
That is awesome, I missed that part in the docs, thanks! I'll update my code 👍🏽 |
I have a generic log fn which is some scenarios on the server I want it to write to disk, this is what I'm using...
I seem to recall "typeof window === 'undefined'" is a magic string that causes segments to be omitted from the client bundle |
In my case, it was a separation of FE and BE code. I had used barrel export in some folders, e.g:
importing FE code by from the index will give this warning:
need to explicitly import or separate the folders Option 1, do not use barrel export:
Option 2, separate FE and BE code:
|
For those trying module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback.fs = false;
}
return config;
},
} Reference: https://webpack.js.org/migrate/5/#clean-up-configuration |
I tried this method and it didn't work, I have a problem with the pg module for postgresql, when I use it in my code it returns an error for fs, native pg and others. when I use this method I always get the same errors, I use version 5 of webpack |
@AIEAJN you're probably exporting the |
ahhh okk i see now, thanks |
I had same issue, this comment helped #7755 (comment) 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. |
I had this same issue (using fs/promises) and applying any of the proposed solutions works: Solution 1: Solution2 module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback.fs = false;
}
return config;
},
} 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? |
Setting next.config.js: target = 'experimental-serverless-trace' |
How do I tell next.js to use webpack 5. I still get this message |
Try this: module.exports = {
webpack5: true,
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback.fs = false;
}
return config;
},
} |
When I add the proposed solution for webpack 5 the error disappears, but subsequent errors arise: Does anyone have any more/other suggestions? I'm running "next": "^11.1.2", |
@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 Edit: My problem turned out to be a stupid mistake on my end:
But getInitialProps runs on the client side so I had to replace it with:
|
I have the same proble with "next": "12.0.4", |
Why is there no mention of this in the Next.js Inside The webpack 5 solution above did in fact fix the issue. Very happy, after several sessions of research. Cheers! |
Keep in mind Next' code elimination only works under |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
I'm using the popular find-up npm package, which has locate-path as a dependency.
locate-path
requiresfs
within it's code.When I attempt to run my app I get the following error message:
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
The text was updated successfully, but these errors were encountered: