.env file example: database URL, port and secret key
Create a .env file for your app: port, database address and a secret key.
A .env file keeps settings and secrets out of your code. Fill in the values below to create one, then save it in your project folder and add it to .gitignore.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
# myapp settings
APP_NAME=myapp
APP_ENV=development
APP_DEBUG=true
PORT=3000
DATABASE_URL=postgresql://app:change-me@localhost:5432/appdb
SECRET_KEY=replace-with-a-long-random-stringHow this setup works
Step-by-step setup
- Save the file as .env in your project folder, next to package.json or manage.py.
- Keep it out of Git: echo ".env" >> .gitignore.
- Load it: Node needs npm install dotenv, Python needs pip install python-dotenv. Laravel loads it for you.
- Make a copy without secrets named .env.example to share with your team.
- Restart your app. It should listen on port 3000.
What the important lines mean
- DATABASE_URL joins user app, password, host localhost, port and database appdb into one address.
- APP_ENV=development and APP_DEBUG=true change how much detail errors show. Turn debug off in production.
- SECRET_KEY must stay private and be long and random.
Common errors and how to fix them
- Variables are undefined
- Your app only reads .env if you load it. Add dotenv or python-dotenv, and start the app from the folder that contains .env.
- Database connection fails with a strange password error
- Characters like @ : / # must be URL-encoded inside DATABASE_URL. The password field only allows safe characters for this reason.
- Secrets ended up on GitHub
- Add .env to .gitignore before your first commit. If it was already pushed, change every secret.
Frequently asked questions
- Where do I put the .env file?
- In the root folder of your project, next to package.json, manage.py or composer.json. Start your app from that folder.
- Should I commit .env to Git?
- No. Add .env to .gitignore and share a .env.example file with the secret values removed.
- How do I load a .env file in Node or Python?
- In Node, install dotenv and call require('dotenv').config(). In Python, install python-dotenv and call load_dotenv().
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
Fix a problem with this setup
- MySQL access deniedFix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
More developer configs
- VS Code settings.jsonSet font, tab size, format on save and theme in your VS Code settings.
- .bashrc and .zshrc aliasesAdd handy shortcuts and settings to your terminal.
- .gitignore fileTick your languages and tools to get a .gitignore that keeps build files and secrets out of Git.
- .editorconfig fileMake every editor on your team use the same indent, line endings and encoding.
- GitHub Actions workflow for Node.jsA ci.yml file that installs, lints, builds and tests your Node.js project on GitHub.
- package.json fileCreate a package.json with the right name, version, module type, scripts and Node version.