How to config multiple environment file in nx monorepo

148 Views Asked by At

I have NX monorepo with multiple nest.js projects, I'm trying to create multiple env files for each app from the custom folder called environment:

  1. local.env
  2. prod.env
  3. dev.env

I tried to check the decimation on NX website but it's evident I searched over all google there is no legal way to do it, and the NX website is showing there is an infrastructure to do it.

I tried to add envFile and env to project.json that did not help

Now my project.json looks like

{
  "name": "be-license-server",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/be-license-server/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "outputs": [
        "{options.outputPath}"
      ],
      "defaultConfiguration": "production",
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/be-license-server",
        "main": "apps/be-license-server/src/main.ts",
        "tsConfig": "apps/be-license-server/tsconfig.app.json",
        "assets": [
          "apps/be-license-server/src/assets"
        ],
        "isolatedConfig": true,
        "webpackConfig": "apps/be-license-server/webpack.config.js"
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "be-license-server:build"
      },
      "configurations": {
        "development": {
          "buildTarget": "be-license-server:build:development"
        },
        "production": {
          "buildTarget": "be-license-server:build:production"
        }
      }
    },
    "lint": {
      "executor": "@nx/linter:eslint",
      "outputs": [
        "{options.outputFile}"
      ],
      "options": {
        "lintFilePatterns": [
          "apps/be-license-server/**/*.ts"
        ]
      }
    },
  "tags": ["prisma"]
}
1

There are 1 best solutions below

0
brk On

You can load the environment files for each of the apps. You need to add files in the fileReplacements array

"production": {
  "budgets": [
    // some code here
  ],
  "fileReplacements": [{
      "replace": "original file",
      "with": "file you want to replace with"
    },

  ],