example-zeit-now-dotenv icon indicating copy to clipboard operation
example-zeit-now-dotenv copied to clipboard

Using dotenv with https://zeit.co/now

Using dotenv with Zeit's Now

You have an app with secrets like database passwords and API keys. You're being responsible by storing that config in the environment (process.env).

index.js

require('dotenv').config()

require('http').createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' })

  res.write(JSON.stringify({
    docs: require('./package.json').homepage,
    src: process.env.NOW_URL + '/_src',
    dns_host: process.env.DNS_HOST
  }, null, 2))

  res.end()
}).listen(8000)

NOW_URL is provided by now to detect now environment

.env

DNS_HOST=california.zeit.world

.gitignore

# Dependency directories
node_modules

# SECRETS
.env

Because now follows the same rules as npm publish for deploys, your .env file will not be included in the deploy. To fix this, create a .npmignore file that explicitly states to include your .env file.

.npmignore

# include secrets when deploying to now
!.env

Alternatively by defining anything else in .npmignore like test/ or .travis.yml, your .env file should be included since .npmignore supersedes .gitignore

See it live!