Dotenv Linter
dotenv-linter is a command-line utility for linting `.env` files, ensuring they adhere to best practices and common standards. It helps prevent syntax errors, enforce naming conventions, and identify issues like duplicate keys or incorrect line endings. The current version is `0.7.0`, with releases typically driven by new features, Python version support updates, and dependency upgrades.
Common errors
-
dotenv-linter: command not found
cause The `dotenv-linter` executable is not in your system's PATH, or the package was not installed correctly in the active environment.fixEnsure `pip install dotenv-linter` completed successfully. If using a virtual environment, activate it. Check your PATH configuration. -
D001: Found `\r\n` (CRLF) as line ending
cause Your `.env` file contains Windows-style CRLF line endings, which are forbidden since version 0.6.0.fixConvert your `.env` file to use LF (Unix-style) line endings. You can often do this in your text editor (e.g., VS Code: 'CRLF' in status bar -> 'LF'), or using `sed -i 's/\r$//' .env` on Linux/macOS. -
Error: The provided argument is not a file or directory.
cause The path provided to `dotenv-linter` (e.g., `.env`) does not exist or is inaccessible.fixDouble-check the filename and path. Ensure the file exists in the current directory or provide the correct absolute/relative path. Also, verify file permissions. -
D002: Found duplicate key `YOUR_KEY`
cause Your `.env` file contains the same environment variable key defined more than once.fixRemove duplicate definitions of the key. Environment variables should typically be defined only once per `.env` file. The last definition often overrides previous ones, but this behavior can be inconsistent across systems.
Warnings
- breaking Older Python versions are regularly dropped. v0.6.0 dropped Python 3.9, v0.5.0 dropped 3.7 and 3.8, and v0.4.0 dropped 3.6. Ensure your environment uses Python 3.10 or newer.
- breaking The linter now explicitly forbids `\r\n` (CRLF) line endings in `.env` files, enforcing `\n` (LF). Files with CRLF will raise a `D001` linting error.
- gotcha The internal grammar parser switched from `ply` to `lark` in version 0.7.0. While this is largely an internal change, it might subtly alter parsing behavior for highly malformed or edge-case `.env` files.
Install
-
pip install dotenv-linter
Quickstart
echo 'APP_ENV=development DATABASE_URL=postgres://localhost:5432/db # This is a comment ' > .env dotenv-linter .env # Example with an error (duplicate key) echo 'FOO=bar FOO=baz' > .env_with_error dotenv-linter .env_with_error