[Question] dotenv dynamic changes for production environment
btd1337 opened this issue · 6 comments
Issue type:
- question
- bug report
- feature request
- documentation issue
How do you make .env
dynamic changes?
Ex:
I have .env
and production.env
. How can I change this using the npm script?
npm run start
npm run start: prod
I am using the nestjs-config package but I am not able to make this change.
Thanks in advance!
How do you make .env dynamic changes?
I don't think it's possible to reload the .env file dynamically when changes are made? It's possible with nodemon I suppose? Never tried it but I don't think that's what you mean?
Yes! You would be able to do that with like so
import { Module } from '@nestjs/common';
import { ConfigModule } from 'nestjs-config';
import * as path from 'path';
import UserController from './user.controller';
@Module({
imports: [
ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}', {
path: path.resolve(path.cwd(), process.env.NODE_ENV === 'production' ? 'production.env' : '.env'),
})),
],
controllers: [UserController],
})
export default class UserModule {}
Just incase NODE_ENV isn't set. You can do this NODE_ENV=production yarn start:prod
I thought this was documented in the README but it's not. So I'm going to use this issue for that.
This don't worked for me:
TSError: ⨯ Unable to compile TypeScript:
src/app.module.ts:18:14 - error TS2339: Property 'cwd' does not exist on type 'typeof import("path")'.
18 path.cwd(),
~~~
What path.cmd()
is this?
Sorry process.cwd()
returns current working dir. You can use whatever you want :)
I've just thought though. You shouldn't be committing your production env file! I think its 'common practise' (or at least as I've seen) to make an .env.dist or .env.example and commit that but still use an .env . I then use the dist/example to make my own env file locally. Then in production I use the env parameters set on the machine itself :) hope this helps!
@bashleigh Any ideas?
Argument of type '{ path: string; }' is not assignable to parameter of type 'string'.
You're passing { path: '' }
where you should be passing a string? :p
@jeffminsungkim could you open a separate issue please, give an example of how you've used the package and what version :) thanks!