dwyl/env2

env2 takes too much time load the variable

Closed this issue ยท 6 comments

I am using env2 for loading env variable in nodejs+graphql setup and call the env loading function at the top of index.js file followed by schema for graphql server.

I am facing a problem where the env2 has not loaded the variable even after the method has been called like just after the line require('env2')(/path/to/env.json).

@mauryakrishna thanks for opening this issue. Could you please share more detail?
Is the require('env2') at the top of your file?
Please console.log(process.env) to confirm that the Environment Variables are loaded.

Consider the below code example:
devenv.json
{
"SET_ENV": 'set_env'
}

index.js
require('env2')(path/to/env.json)

import othermodule from './othermodule';

othermodule.js
console.log('Two', process.env.SET_ENV); //prints undefined, expected was the value fromenv

I think the issue title I put was not giving an exact sense of it. It should have been env not loading in synchronise manner .

Extremely sorry. There is no problem with module. My Bad.

Node.js' require is synchronous the .env2 code is loading the file using fs.readFileSync

return JSON.parse(fs.readFileSync(path))

@mauryakrishna out of curiosity, what made you realise that the issue was not with env2?

I am using esm to work write my code into ES6+. So I just commented all the code and noticed the sequence of files loading with all sync code.

Removing the esm and using require worked.
So tried removing the import statement and using require things worked with esm. Then I was confirm that esm is transforming the code to es5 before it goes for execution.

I am still using the esm as it did not require any kind of setup to get up. And just used the hack of require instead of import for that single line.