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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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!
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.
> "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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.