Skip to content
Not loading env var on deployed production
brenoscosta
OP

2 years ago

I have a JWT_KEY env var that i need to use to decrypt the access token, if i ship as "JWT_KEY" it always returns undefined, if i use "NEXT_PUBLIC_..." i get the value.

The issue is that if i use the NEXT_PUBLIC it will be available on the browser, and i dont want that since is a sensitive information.

I only use this key on one file "decrypt.ts" that is almost identical from the nextjsdocs: https://nextjs.org/docs/app/building-your-application/authentication#2-encrypting-and-decrypting-sessions

- Nextjs 15.1.7

- A monorepo structure with turborepo with the following

├── apps/ # Main applications

│ └── web/ # Web application

│ ├── app/ # Next.js app directory

│ ├── lib/ # Library code

│ decode.ts

│ .env

├── packages/ # Shared packages/libraries

│ ├── icons/ # Icon components

│ └── ui/ # UI component library

Solved

5 Replies

2 years ago

This is intended behaviour for Next applications. If you're trying to read any variables that isn't non-NEXT_PUBLIC_* on the client, it won't show up. For security reasons, you can only make variables available to Next on the client via NEXT_PUBLIC_*. If you're not getting an env var you set, it might be that you're attempting to read it somewhere on the client instead of on the server (where it would be available; e.g. Next middlewares on app router).

Unfortunately this is specific to the framework you're using (Next) and not the Railway platform, so we're unable to help you out further.


Status changed to Awaiting User Response Railway • over 1 year ago


Status changed to Solved ray-chen • over 1 year ago


brenoscosta
OP

2 years ago

i am only using the var on the decrypt.ts file that i am using the "import 'server-only'", the decrypt i am using in the session.ts that is marked with the 'use server', i am using the getSession function from the session.ts in my page(s).tsx, that all of then are server rendered and passing the user information via props for the client components, i also have a HOC component that wraps all the pages that manage the access to then.. and i checked it is still server rendered

still dont see why i cant use the var without the prefix


Status changed to Awaiting Railway Response Railway • over 1 year ago


brenoscosta

i am only using the var on the decrypt.ts file that i am using the "import 'server-only'", the decrypt i am using in the session.ts that is marked with the 'use server', i am using the getSession function from the session.ts in my page(s).tsx, that all of then are server rendered and passing the user information via props for the client components, i also have a HOC component that wraps all the pages that manage the access to then.. and i checked it is still server rendered still dont see why i cant use the var without the prefix

brenoscosta
OP

2 years ago

and locally it works if i use without the prefix

only the deployed version dont work


2 years ago

Since you are using turbo, please see their docs on this topic -

https://turbo.build/docs/crafting-your-repository/using-environment-variables#environment-modes

You are likely using turbo in Strict Mode.


Status changed to Awaiting User Response Railway • over 1 year ago


brody

Since you are using turbo, please see their docs on this topic - <https://turbo.build/docs/crafting-your-repository/using-environment-variables#environment-modes> You are likely using turbo in Strict Mode.

brenoscosta
OP

2 years ago

Fixed it, I forget to put the vars on the "globalEnv".

thanks


Status changed to Awaiting Railway Response Railway • over 1 year ago


Status changed to Solved brenoscosta • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...