logoNavigate back to the homepage




July 1st, 2020 · 2 min read


Let’s face it, your coding environment plays a major role in how your application operates (pun intended)!

Managing environment variables is ever more important in today’s containerized and cloud native applications, be it for defining your database connection string per environment or storing your access keys. Using a .env file is a common practice for managing these variables and dotenv presents a simple and zero-dependency way to use this method in your JS code.


The package is extremely simple to use, requiring only one line of code and the .env file in majority of the use cases. The package expects a .env file in your project’s root directory and injects the variables in the file into the node’s process.env object when instantiated as below:


For example, using a value DATABASE_CONN_STRING="test_protocol://test:test@test:5432" present in your .env file, post instantiating the config, would be:


For proper usage of the package, we should make sure that the config is loaded only once and at the earliest step of code execution

The config method mentioned above does accept and object as argument for further config and the accepted keys at this point include path for defining a custom path for the .env file if it’s not in your project root directory, encoding for defining the .env file content encoding and debug for enabling debug message logging to console.

Preloading of the package is also supported, negating the need to instantiate it inside application code by calling the config method and is preferred when using import. The config options with this method can be defined using command line argument of the format dotenv_config_<option>=value.

Though defining different .env files for each environment is not advised, this option does come handy when tinkering with the code, for example to quickly switch between different cloud hosting options when you are trying to analyse the performance of your APIs in each of them. In the below example, we define the credentials and API paths and use it with preloading as below:

1node -r dotenv/config script_to_run.js dotenv_config_path=/path/to/aws.env/file
2node -r dotenv/config script_to_run.js dotenv_config_path=/path/to/azure.env/file

The environment file parser can also be accessed by calling the parse method on the import object. This is useful for manually parsing and loading config variables, say from in memory files or buffers, as an object into a variable.

For example:

1const dotenv = require('dotenv')
2const tempBuffer = Buffer.from('VAR=value')
3const config = dotenv.parse(tempBuffer) // config will contain { VAR : 'value' }

The parse method also supports a config object as the second argument and the rules governing the parser are quite extensive and can be found in the docs

Evaluation metrics

Ease of useGoodExtremely simple syntax, cross platform support
CommunityGoodFew open issues, under active development
Active usageGoodMore that 8M weekly downloads at the time of writing, more than 16K dependent packages on npm
VulnerabilitiesGoodNo vulnerabilities reported by npm audit
Docs and TrainingsGoodDecent docs covering all use cases, many third-party articles for usage scenarios explanation

Check out the package and some reading materials

Video review of the package

Video review of the package with interesting use cases and in-depth exploration of the features coming soon!


The content and evaluation scores mentioned in this article/review is subjective and is the personal opinion of authors at Unpackaged Reviews based on everyday usage and research on popular developer forums. They do not represent any company’s views and is not impacted by any sponsorships/collaboration.

Header Photo by Lukasz Szmigiel on Unsplash

More articles from Unpackaged Reviews


An easy way to git-hooked!

July 1st, 2020 · 2 min read


Adding unique identities made easy!

June 25th, 2020 · 3 min read
© 2020-2021 Unpackaged Reviews
Link to $https://twitter.com/unpakgd_reviewsLink to $https://unpackaged-reviews.medium.com