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:Jump to 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.
16 Replies
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 »
You can use server-only package. It will throw an error if client dependency is used. This is recommended by official nextjs docs.
Can u link where it was used? Thanks :D
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-packageGetting Started: React Essentials
An overview of essential React features for building Next.js Applications, including Server Components.
also for some packages
like bcrypt
I don't really understand your need. Are you talking about Next.js framework or something else?
I am talking about next js
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.ok, so like for example, I accidentally import bcrypt in my client components, it wont be bundled for the client?
as long as it's not used in any client component, yes
I see
Thanks all :D
Try to use
fs
interactions or even with your database from a use client
file, you'll see what happensalso, what are some things I can do to optimize my app?
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
ohh ok
Thanks