2

I have three environments (local, test and prod) in my NodeJS based server project in my nx workspace. I require three different environment.ts files.

I have the following configuration in my workspace.json for my NodeJS project:

"configurations": {
            "production": {
              "optimization": true,
              "extractLicenses": true,
              "inspect": false,
              "fileReplacements": [
                {
                  "replace": "apps/api/src/environments/environment.ts",
                  "with": "apps/api/src/environments/environment.prod.ts"
                }
              ]
            },
            "test": {
              "optimization": true,
              "extractLicenses": true,
              "inspect": false,
              "fileReplacements": [
                {
                  "replace": "apps/api/src/environments/environment.ts",
                  "with": "apps/api/src/environments/environment.test.ts"
                }
              ]
            }
          }
        },

The environment files sit in the same hierarchy:

The environment files sit in the same hierarchy

I am unable to find which command will pick up the test configuration. I have tried multiple things it did not work:

nx build api --test # does not work
NODE_ENV=test nx build api # does not work
NODE_ENV=test nx build api --test # does not work
nx build api -c=test # does not work
nx build api -c test # does not work
nx build api --prod # works for prod env file

Expected Behavior

I should be able to choose between different environment files during the build.

Current Behavior

fileReplacement does not seem to work for NodeJS project for anything other than prod.

Steps to Reproduce

In your workspace with one server side project, say api, do the the following: 1. Add a new file environment.test.ts on the same pattern as the existing environment.prod.ts 2. In the workspace.json under the "api" > "architect" > "buiid" > "configurations" block, add a new configuration for the "test" environment that looks like the existing "production" configuration, except it has "fileReplacements" array that looks like this:

               {
                  "replace": "apps/api/src/environments/environment.ts",
                  "with": "apps/api/src/environments/environment.test.ts"
                }

Now try to do a build that picks up and replaces environment.ts with environment.test.ts during the build. It does not seem to be possible.

The commands I tried above did not help me. Here is my nx version.

nx --version
8.9.0
  1. Relevant issue I raised here: https://github.com/nrwl/nx/issues/2870
  2. Similar concerns were raised here: Using environment variables in nx based nodejs app
Nishant
  • 54,584
  • 13
  • 112
  • 127

1 Answers1

2

The command is nx build api --configuration=test or nx build api --c=test

If you are using Nx Console VS Code plugin, you can select one of the options from the top under the build menu. The resulting command is exactly the same as mentioned above. Here is the screenshot for clarity.

How to select environment in Nx Console VS Code GUI

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • Kinda confused here. If I am using the Nx Console GUI with VS cold, which flag are we talking about? I don't see "configuration=test" – brando Dec 06 '20 at 20:04