How to run code that imports "server-only" from the command line
Unanswered
American black bear posted this in #help-forum
American black bearOP
Hello! I have several files that access the database and I've imported "server-only" to avoid them being imported by client code.
I also have code that is run from the command line (specifically to seed the database) and that imports functions from these files, however I'm getting this error:
Is there any way to set some environment variable so it believes it's running in the server?
Thank you!
I also have code that is run from the command line (specifically to seed the database) and that imports functions from these files, however I'm getting this error:
throw new Error(
^
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.Is there any way to set some environment variable so it believes it's running in the server?
Thank you!
15 Replies
@American black bear easiest method is to try and get any env variables
If it's undefined, you are in client
If it returns a value, you are in server
Your env variables must not be prefixes with NEXT_PUBLIC, and the variable must exist
American black bearOP
Hi Arinji!
Maybe I didn't explain myself well. Lets say I have the following file:
I want to call
In package.json scripts:
and doSomethingWithDb.js imports the
Maybe I didn't explain myself well. Lets say I have the following file:
import "server-only"
const myFunction = (db) => {
// Do something with db
}I want to call
myFunction from a command line script:In package.json scripts:
"doSomething": "node doSomethingWithDb.js",and doSomethingWithDb.js imports the
myFunction fileI get the error I mention above.
I have a feeling server-only dosent account being called by node itself as on a server environment
American black bearOP
Yes exactly. My thought was maybe it checks for some ENV variable to be set or not. So if I set it it would work. But unsure which, if this is a solution
Honestly 0 clue about why server-only won't consider node as a server, you can remove it and try the environment thing for checking if you are on the server.. or else wait for someone with the knowledge regarding the same
American black bearOP
I want to seed the database (see https://www.prisma.io/docs/orm/prisma-migrate/workflows/seeding)
So via the command line I run a script that uses existing backend code to generate users
I think it'd be better to create a js file outside the project, if you're facing this issue
@American black bear Hello! I have several files that access the database and I've imported "server-only" to avoid them being imported by client code.
I also have code that is run from the command line (specifically to seed the database) and that imports functions from these files, however I'm getting this error:
throw new Error(
^
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.
Is there any way to set some environment variable so it believes it's running in the server?
Thank you!
use
--conditions for the node command. reference: https://nodejs.org/api/packages.html#resolving-user-conditions