0

in the file of app.module.ts, I used Environment variable

  imports: [
    ConfigModule.forRoot({ envFilePath: [`./src/config/${process.env.NODE_ENV}.env`], isGlobal: true },),
    MongooseModule.forRoot(process.env.DATABASE_URL, {
      useNewUrlParser: true,
      user: `${process.env.DATABASE_USER}`,
      pass: `${process.env.DATABASE_PASSWD}`,
    }),

It was working when I test in local,But when I build it,The env is not work,So what can I do ,Thinks

my package.json script

"build": "NODE_ENV=prod nest build", // the env not work
"start:dev": "NODE_ENV=dev nest start --watch",  // it works
tomsma
  • 39
  • 5

1 Answers1

1

build and therefor nest build does not run the code, it runs the compiler to transform the ts files into js files, so there's never any check or evaluation of what process.env.WHATEVER is, it's just a translation from ts syntax (with all the types) to js syntax that can be interpreted by node.

start:dev (mapped to nest start --watch) is what is actually running the code, using node as the JavaScript engine.

You probably are wanting something like

"start:dev:prod": "NODE_ENV=prod nest start --watch"

Or you can modify the start:prod command Nest provides in new projects to be NODE_ENV=prod node dist/main.js

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • I think you meant `"start:prod"` instead of `"start:dev:prod"`. – Uroš Anđelić Feb 04 '21 at 09:08
  • No, I meant making a new script `start:dev:prod` to map to `NODE_ENV=prod nest start --watch` because Nest projects already have a `start:prod` like I mentioned in the second half of my answer. This new script would allow for hot reloading with the `prod` value for `NODE_ENV` – Jay McDoniel Feb 04 '21 at 16:23