I'm currently working on a nestjs project. and the developer of the project has used a .env file to keep the app path and some other required information (i don't have access to this file) in the code he has accessed the attributes of .env file in two different ways.
first way
if (process.env.NODE_ENV === 'development') {
app.enableCors({ origin: 'http://localhost:3000' });
}
second way
PDFModule.register({
isGlobal: true,
view: {
root: join(process.env["APP_PATH"], 'resources/pdf'),
engine: 'mustache',
},
}),
i want to what is different in using process.env.NODE_ENV
and process.env["APP_PATH"]
to access attributes in .env file? and how can i specify app path correctly in .env file.
CodePudding user response:
yeah i know what value to assign it but what i'm asking is the difference between two access methods with . and []
The []
you are mentioning is the Computed Property names was introduced in ECMAScript 2015. So there are 2 ways to access an object property - using []
and .
Also []
is handy when you don't know the environment variable NODE_ENV
but its available in another variable.
Example let myEnvVariable = "NODE_ENV";
In this case process.env[myEnvVariable]
is same as process.env.NODE_ENV
or process.env["NODE_ENV"]