How can i make sure dotenv is loaded


So, the dotenv config is placed below the requires, how can i make sure it will always be above?

You can use @@directive feature which puts its contents above everything else:

@@directive(`require("dotenv")`)
3 Likes

You mean the Dotenv.config() call? It shouldn’t be above requires (not before require("dotenv") at least), or you’ll end up trying to access a property of Dotenv before it’s assigned a value.

Consider dotenvx - also from the creator of dotenv.

so if the start command in package.json was previously:

"start": "node src/Server.bs.js",

it would now be:

"start": "dotenvx run -- node src/Server.bs.js",

No need for any extra dotenv.config initialization step in your app code.

Or alternatively if you don’t mind experimental node features there’s also:

node --env-file=.env app.js
2 Likes