How to use module that has `import "server-only"` outside Next.js?
Answered
Atlantic cod posted this in #help-forum
Atlantic codOP
Suppose I have an
and I use this file within my Next.js application normally. I want to use the same file with my
server-thing.ts file that looks like the following:import "server-only"
export const SOME_SERVER_THING = "...."and I use this file within my Next.js application normally. I want to use the same file with my
drizzle.config.ts file which is outside of Next.js. But because it imports server-only which throws an error by default, I am unable to use it. What can I do about it? I don't want to remove all uses of server-only from my app or even the file.Answered by Atlantic cod
I found a fix. The solution is to use the
Also, the files I want to include already use environment variables. I just don't want server code ever accidentally getting into client code. Thanks anyways
server-cli-only package which uses package.json conditional exports to make the code work on the server normally.Also, the files I want to include already use environment variables. I just don't want server code ever accidentally getting into client code. Thanks anyways
8 Replies
Asian black bear
Do yourself a favor and use this: https://env.t3.gg
@Asian black bear Do yourself a favor and use this: https://env.t3.gg
Atlantic codOP
My environment variables are typesafe, or at least they are ok for me. I don't need that. The issue is with
server-only, not environment variablesAsian black bear
That package prevents leaking server-side variables to the client which is something you seemingly attempt to do with your custom solution.
Unless your example is just implying that by accident.
@Asian black bear That package prevents leaking server-side variables to the client which is something you seemingly attempt to do with your custom solution.
Atlantic codOP
No. I want to use the server file in my drizzle config. That isn't a client thing.
@Asian black bear Unless your example is just implying that by accident.
Atlantic codOP
Yeah, maybe. the config is for running migrations and push db schema updates so it's not a client thing.
I just basically want it to work as I might need it for something else later on.
I just basically want it to work as I might need it for something else later on.
@Atlantic cod Suppose I have an `server-thing.ts` file that looks like the following:
ts
import "server-only"
export const SOME_SERVER_THING = "...."
and I use this file within my Next.js application normally. I want to use the same file with my `drizzle.config.ts` file which is outside of Next.js. But because it imports `server-only` which throws an error by default, I am unable to use it. What can I do about it? I don't want to remove all uses of `server-only` from my app or even the file.
tldr not possible. move away from hardcoded secrets in the code and use proper environment variables with env.t3.gg as near said.
---
if a
that
you will have to figure out how to add this
---
if a
script.js file imports something that imports server-only, you need to run it like sonode --conditions=react-server script.jsthat
conditions is important to tell nodejs where to look for the file.you will have to figure out how to add this
--conditions to the drizzle commands all by yourself. all things considered, it will take significantly more work this way than if you just do the normal thing and use environment variables.@joulev tldr not possible. move away from hardcoded secrets in the code and use proper environment variables with env.t3.gg as near said.
---
if a `script.js` file imports something that imports `server-only`, you need to run it like so
node --conditions=react-server script.js
that `conditions` is important to tell nodejs where to look for the file.
you will have to figure out how to add this `--conditions` to the drizzle commands all by yourself. all things considered, it will take significantly more work this way than if you just do the normal thing and use environment variables.
Atlantic codOP
I found a fix. The solution is to use the
Also, the files I want to include already use environment variables. I just don't want server code ever accidentally getting into client code. Thanks anyways
server-cli-only package which uses package.json conditional exports to make the code work on the server normally.Also, the files I want to include already use environment variables. I just don't want server code ever accidentally getting into client code. Thanks anyways
Answer