1

I'd like to load some environment variables into functions within my libs, and then be able to re-export this to several different Nextjs applications. i.e.

Within libs/api

export const getDatabaseConnection = () => {
  const host = process.env.DB_HOST
  const username = process.env.DB_USERNAME
  ...
  return newDatabaseConnection
}

Within apps/myNextJSApp:

import { getDatabaseConnection } from '@myProject/api'

...
const databaseConnection = getDatabaseConnection()
...

When I run nx run myNextJSApp:serve it's unable to pull the environment variables from the .env within the root directory, however if I run nx run api:test it's completely happy. I think I could pull the environment variables from each app individually, and then pass them as params into my library functions, but this seems kind of tedious, and I was hoping theres a blanket solution to this where I could build my library modules with the environment variables, and export them to my NextJS apps.

reeslabree
  • 127
  • 13

1 Answers1

6

Environment variables don't suppose to share by multiple apps. And it's better that you don't do it either. Every app you're creating should have its environment file. And don't read the environment from your libs directly. You must load the environment variable in your app and pass it to the lib.

For example, pass the needed config to the libs function:


// libs/api
export const getDatabaseConnection = ({host, username}) => {
  const host = host
  const username = usename
  ...
  return newDatabaseConnection
}

// apps/nextjs
import { getDatabaseConnection } from '@myProject/api'

...
const databaseConnection = getDatabaseConnection({
  host: process.env.DB_HOST,
  username: process.env.DB_USERNAME
})

This way, your code is more reusable and maintainable.
And as I said, don't ever share your environment variables between apps.

Ali Shabani
  • 428
  • 2
  • 10
  • gotcha - in that case, there should not be a mono-repo level `.env`, rather `apps/nextjs1/.env`, `apps/nextjs2/.env`, `apps/anotherapp/.env`, etc. Thanks for the quick reply! – reeslabree Aug 15 '22 at 21:37
  • Yes, that is true. And don't forget to ignore .env files in your git. Put a sample .env.sample in every app in your repository instead with example values. – Ali Shabani Aug 15 '22 at 22:09
  • > **"don't ever share your environment variables between apps"** … I feel like that statement needs some qualification or citation. For example, perhaps a monorepo has many apps that use a lot of the same environment variables; in that case is it really a bad idea to have one shared environment file? – Peter W Apr 20 '23 at 09:40
  • there is no strict rule in programming, your limitation is your imagination. if you find a way to deal with shared and private environment variable in monorepo at the same time you can do it. but I think duplicating the shared ones worth it. it's less confusing, and make your apps more secure against some leaked sensitive data. – Ali Shabani Apr 21 '23 at 13:51