Server only dependencies

Hi, I wanted to ask if there is something like server only dependencies, for example, I don't want to be able to use or even import something like bcrypt on the client side, and also, it shouldn't be included in the client bundle... How can I achieve that?
Solution:
Do you need server-only? Using it makes sure your file is never imported from the client. https://nextjs.org/docs/getting-started/react-essentials#the-server-only-package...
Getting Started: React Essentials
An overview of essential React features for building Next.js Applications, including Server Components.
Jump to solution
16 Replies
Steven-sensei
Steven-senseiβ€’2y ago
I don’t know about the import part but you client bundle wont include backend lib as long as you aren’t importing it in a client component and « use itΒ Β»
dickinsontiwari
dickinsontiwariβ€’2y ago
You can use server-only package. It will throw an error if client dependency is used. This is recommended by official nextjs docs.
TheBassGuy πŸŽ›πŸŽš
Can u link where it was used? Thanks :D
Solution
Benjamin
Benjaminβ€’2y ago
Do you need server-only? Using it makes sure your file is never imported from the client. https://nextjs.org/docs/getting-started/react-essentials#the-server-only-package
Getting Started: React Essentials
An overview of essential React features for building Next.js Applications, including Server Components.
TheBassGuy πŸŽ›πŸŽš
also for some packages like bcrypt
Benjamin
Benjaminβ€’2y ago
I don't really understand your need. Are you talking about Next.js framework or something else?
TheBassGuy πŸŽ›πŸŽš
I am talking about next js
Benjamin
Benjaminβ€’2y ago
In Next.js, the bundled JS sent to the client is limited and such server package is never included. server-only implicitely makes sure those imported packages in your code are never bundled in the client. You can try it, add the import in your file and check the bundled JS, you will have no reference to your package.
TheBassGuy πŸŽ›πŸŽš
ok, so like for example, I accidentally import bcrypt in my client components, it wont be bundled for the client?
Benjamin
Benjaminβ€’2y ago
as long as it's not used in any client component, yes
TheBassGuy πŸŽ›πŸŽš
I see Thanks all :D
Benjamin
Benjaminβ€’2y ago
Try to use fs interactions or even with your database from a use client file, you'll see what happens
TheBassGuy πŸŽ›πŸŽš
also, what are some things I can do to optimize my app?
Benjamin
Benjaminβ€’2y ago
first, if you respect the official documentation, they give very good advices about best practices like for app router, how to use server and client component efficiently
TheBassGuy πŸŽ›πŸŽš
ohh ok Thanks
Benjamin
Benjaminβ€’2y ago
Then, when you're testing your app, just monitor the logs, use the debug mode everywhere you can when in development mode (like your database access, etc.), it's really helpful to see how the network and caching are behaving The doc covers the usual pitfalls we tend to fall in πŸ‘

Did you find this page helpful?