Quickstart

Load .env files in development

Load your .env files in development using this quickstart guide.

Create .env file

Create a .env file.

.env

# development
HELLO="World"

Install dotenv

Install dotenv in the language of your choice.

npm install dotenv --save

Require dotenv

Write your application code and require dotenv.

Here we are creating a simple Hello World server and requiring the dotenv library first - before any other code runs.

// index.js
require('dotenv').config()
const PORT = process.env.PORT || 3000
const http = require('http')
const server = http.createServer((req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/plain')
  res.end(`Hello ${process.env.HELLO}`)
})

server.listen(PORT, () => {
  console.log(`Server running on port:${PORT}/`)
})

Note the code written as Hello ${process.env.HELLO}. This is where dotenv loads HELLO=World form the .env file and injects it into your process env.

Run application

Run your application.

node index.js
# visit http://localhost:3000

If successful, you will see "Hello World".

Conclusion

That's it! You now understand the basics of using dotenv in development. I recommend learning how to deploy .env.vault in production next.

Thanks for using Dotenv!


FAQ

Should I commit my .env file?

No. We strongly recommend against committing your .env file to version control. It should only include environment-specific values such as database passwords or API keys. Your production database should have a different password than your development database and we recommend storing those values in a .env.vault file.