Skip to content
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

Using backend-specific modules in getInitialProps #7730

Closed
germansokolov13 opened this issue Jul 2, 2019 · 2 comments
Closed

Using backend-specific modules in getInitialProps #7730

germansokolov13 opened this issue Jul 2, 2019 · 2 comments

Comments

@germansokolov13
Copy link

I am not sure if it's a correct place to ask this. I am also not sure if it's a bug, feature request or just question. Sorry if I am wrong. I don't think community can help me much here.

Let's suppose I have a page. The page can be rendered server-side or we can navigate to it from within the client. Suppose that there is a reason to do some extra work in case we render the page server-side using HTTP Request data. Suppose we don't need this work to be done on client-side page renders.

Now I also need this to be done in the getInitialProps hook. And I use fs module to get the work done. This is where the problem comes: trying to import fs module into a component leads to error:
ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in ...

Next.js allows the same code to run on both client and server. However it seems it can't run server-specific modules even if it's on server.

Why is that so and how does one work around this?
There is another closed related question here:
#4552
Should an API always be used in such cases? I think it might be redundant in some cases.

@timneutkens
Copy link
Member

timneutkens commented Jul 2, 2019

Yes you should always create an API, soon you'll be able to create an API in Next.js: #7297

Please follow the issue template in the future.

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants