question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nx build does not replace environment file in any variation

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
naishecommented, Apr 20, 2020

Apologies. The --configuration worked. For some reason when I tried --configuration yesterday following the documentation (it was documented under Angular project), it worked for the React project but it did not seem to work for NodeJS. It is totally my fault.

Thanks for the suggestion.

1reaction
rarmateicommented, Apr 20, 2020

can you please try nx build api --configuration=test ?

If not, can you please create an example repo showing this problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

nwrl nx environment files is not replaced for NodeJS project ...
I should be able to choose between different environment files during the build. Current Behavior. fileReplacement does not seem to work for ...
Read more >
Using environment variables in Angular applications - Nx
Using environment variables in Angular applications. By default, Angular only provides the NODE_ENV variable when building the application. You may use ...
Read more >
nrwl-nx/community - Gitter
Hey guys, NX environments does not appear to be a suitable replacement for react (non-angular) .env environment variables, as it offers replacement instead ......
Read more >
Environment variables - Prisma
An environment variable is a key value pair of string data that is stored on your machine's local environment. Refer to our Environment...
Read more >
Configuration | NestJS - A progressive Node.js framework
Since configuration variables change, best practice is to store ... If you don't want to load the .env file, but instead would like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found