-
-
Notifications
You must be signed in to change notification settings - Fork 929
Documentation: Added a clean ES6 (ESM) usage example #1002
Copy link
Copy link
Open
Description
Hi there!
I noticed in the README that you're looking for a good sample for ES6 (ESM) usage.
Since ESM is becoming the standard for Node.js, here is a clean and modern example
of how to use dotenv with the import syntax.
1. Update package.json
Add "type": "module" to your package.json to enable ES Modules:
{
"type": "module",
"dependencies": {
"dotenv": "^16.4.5"
}
}
2. Usage in index.js
The most elegant way to ensure variables are loaded before your app logic:
JavaScript
// This one-liner imports and executes config() immediately
import 'dotenv/config';
// Now you can access your variables
const dbUrl = process.env.DATABASE_URL;
const port = process.env.PORT || 3000;
console.log(`🚀 Server running on port ${port}`);
Why this approach?
Using import 'dotenv/config' is the cleanest way to guarantee that environment
variables are available even for other imported modules that might depend on them.
Hope this helps with the documentation!Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels