Next.js Discord

Discord Forum

Next.js build in a NX Monorepo

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
Hi!
Hope all is well.
I'm facing a weird bug when deploying my Next.js app to production.
I use NX to run a monorepo of different apps and libs.
The app i'm deploying, uses different libs (as you can see) in the picture.
In one of those libs (utils), there is a file I use to manage constant values throughout my apps, that is based on env vars.
These constant values are afterwards for things like auth and such.
An example is the following:
export const IS_PRODUCTION = process.env.NODE_ENV === 'production'

// if one of these is undefined, the app will crash
if (!process.env.SAHL_URL) throw new Error('Missing SAHL_URL')

When building, there isn't any issue, since my env vars are set at build time. I use docker to build & wrap the code, and deploy on a remote server with the right env vars set up. Yet, when running my app in the browser, I run into this issue:
Error: Missing SAHL_URL
    at 7795 (23-75ec8defd7e8853b.js:1:3246)
    at s (webpack-9a7722994228143f.js:1:151)
    at 35172 (layout-3020632d7ea526fa.js:1:1668)
    at s (webpack-9a7722994228143f.js:1:151)
    at x (705-acf7e8db3804a89e.js:1:84048)
    at C (705-acf7e8db3804a89e.js:1:83225)
    at 705-acf7e8db3804a89e.js:1:88255
window.console.error @ 705-acf7e8db3804a89e.js:1

For some reason, the SAHL_URL env var wasn't reaching the code (which seemed to be running client-side, I believe, since I could see it in the browser, minified), even though I could see it if running the following in my container:
> node -e "console.log(process.env.SAHL_URL)"
<MY URL>

I guess my question is the following. For people using monorepos and such, do you configure your webpack or next.config.js in a certain way to manage this situation in a different way?
Or is this a normal behavior? Should I expose those env vars as NEXTPUBLIC*
Thanks!

1 Reply

Giant pandaOP
up (just raising awareness)