Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

chore(env): load .env.local through dotenvx, encrypted or plaintext (#284)

Vlad Sazonauc0738c230c7c
diff --git a/.env.example b/.env.example
index a9b703b..f815b8f 100644
--- a/.env.example
+++ b/.env.example
@@ -1,4 +1,5 @@
# run `cp .env.example .env.local`
+# optionally encrypt it: `pnpm exec dotenvx encrypt -f .env.local` (https://dotenvx.com/)
# for server side
SENTRY_ENABLED="false"
diff --git a/.gitignore b/.gitignore
index 171d9a6..e17efbb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,8 +25,10 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
-# local env files
-.env*.local
+# local env files: every .env* (plaintext or dotenvx-encrypted, including the dotenvx
+# private-key file .env.keys). Only the template is committed.
+.env*
+!.env.example
# vercel
.vercel
diff --git a/CLAUDE.md b/CLAUDE.md
index f80f2c6..e2e4d9e 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -52,6 +52,8 @@ pnpm e2e e2e/invoice-form.test.ts --project="Desktop Chrome" -g "adds an item"
The pre-commit hook (`lint-staged.config.js`) runs `type-check:fast`, `lint`, `zizmor`, `knip`, `vitest --run` and `format` on _any_ staged change, so keep all of those green.
+Env vars come from `.env.local`, which may be encrypted with [dotenvx](https://dotenvx.com/) (check for `encrypted:` values; the private key then lives in the OS keychain or a gitignored `.env.keys`). The `dev`, `build`, `start`, `type-check` and `lint` scripts decrypt it through `pnpm with-env <cmd>` (`dotenvx run --strict`, which stops on a decryption failure), so use those and never bare `next ...`, which would read the raw `encrypted:` strings. If the file is encrypted, change values with `pnpm exec dotenvx set KEY value -f .env.local` rather than editing it, then restart `pnpm dev` — Next's env hot-reload re-reads the encrypted file, so new keys arrive as ciphertext and changed ones stay stale until restart. Don't put `NODE_ENV` in it — `dotenvx run` would inject it into `next dev`/`next start`.
+
`SKIP_ENV_VALIDATION=true` is only for `type-check`/`lint` in CI — never set it for `next dev` or `next build`; `next.config.mjs` imports `src/env.ts` on purpose so a missing env var fails the build.
## Architecture
diff --git a/README.md b/README.md
index 21ee821..2b3e7d7 100644
--- a/README.md
+++ b/README.md
@@ -250,8 +250,11 @@ EasyInvoicePDF is [AGPL v3](#license); self-host your own instance locally or on
- Run `corepack install`
- Run `pnpm install`
- Copy `.env.example to .env.local` (`cp .env.example .env.local`)
+- (Optional) Encrypt it with [dotenvx](https://dotenvx.com/): `pnpm exec dotenvx encrypt -f .env.local`
- Run `pnpm run dev`
+Scripts load `.env.local` through `dotenvx run` (the `with-env` script), so it works plaintext or encrypted. Edit an encrypted file with `pnpm exec dotenvx set KEY value -f .env.local`, read a value back with `pnpm exec dotenvx get KEY -f .env.local`, and restart `pnpm dev` afterwards: Next.js reloads `.env.local` on its own, and from an encrypted file it gets the raw `encrypted:...` strings. If the private key is missing (new machine, locked keychain), scripts stop with `DECRYPTION_FAILED` instead of running.
+
For the full app experience, you’ll need to obtain and set values from the following services:
- [Resend](https://resend.com/)
diff --git a/package.json b/package.json
index 634f0e3..daa8d33 100644
--- a/package.json
+++ b/package.json
@@ -18,12 +18,13 @@
"homepage": "https://easyinvoicepdf.com",
"author": "Vlad Sazonau (https://vladsazon.com)",
"scripts": {
- "dev": "next dev",
- "build": "next build",
- "start": "next start",
- "type-check": "next typegen && tsc --noEmit --diagnostics",
- "type-check:fast": "next typegen && oxlint . --type-check --quiet",
- "lint": "next typegen && oxlint . --type-check --type-aware",
+ "with-env": "dotenvx run -f .env.local --ignore=MISSING_ENV_FILE --strict --",
+ "dev": "pnpm with-env next dev",
+ "build": "pnpm with-env next build",
+ "start": "pnpm with-env next start",
+ "type-check": "pnpm with-env next typegen && tsc --noEmit --diagnostics",
+ "type-check:fast": "pnpm with-env next typegen && oxlint . --type-check --quiet",
+ "lint": "pnpm with-env next typegen && oxlint . --type-check --type-aware",
"lint:fix": "oxlint . --fix",
"format": "oxfmt .",
"format:check": "oxfmt --check .",
@@ -46,10 +47,11 @@
"update-github-actions": "pnpm actions-up --min-age=7",
"check-github-actions-security": "zizmor .",
"test-gh-actions-locally": "act --secret-file .github/workflows/.secrets",
- "trigger:dev": "pnpm dlx trigger.dev dev",
+ "trigger:dev": "pnpm with-env pnpm dlx trigger.dev dev --env-file /dev/null",
"trigger:deploy": "pnpm dlx trigger.dev deploy"
},
"dependencies": {
+ "@dotenvx/dotenvx": "2.34.2",
"@hookform/resolvers": "5.9.1",
"@mdx-js/loader": "3.1.0",
"@mdx-js/react": "3.1.0",
@@ -120,7 +122,6 @@
"@types/react-dom": "19.2.4",
"actions-up": "1.14.2",
"autoprefixer": "10.4.21",
- "dotenv": "17.2.3",
"eslint-plugin-playwright": "2.11.0",
"eslint-plugin-react": "7.37.5",
"eslint-plugin-react-you-might-not-need-an-effect": "1.0.2",
diff --git a/playwright.config.ts b/playwright.config.ts
index 6ad81a3..6c82147 100644
--- a/playwright.config.ts
+++ b/playwright.config.ts
@@ -1,11 +1,11 @@
import path from "node:path";
+import { config as loadEnvFile } from "@dotenvx/dotenvx";
import {
defineConfig,
devices,
type PlaywrightTestOptions,
} from "@playwright/test";
-import dotenv from "dotenv";
import { getLatestChangelogSlug } from "./e2e/utils/latest-changelog-slug";
import {
@@ -16,10 +16,19 @@ import {
import { SENTRY_E2E_DISABLED_STORAGE_KEY } from "./src/lib/sentry/sentry-e2e-flags";
/**
- * Read environment variables from file.
- * https://github.com/motdotla/dotenv
+ * Read (and decrypt) environment variables from file.
+ * https://dotenvx.com/docs
+ * Workers inherit the runner's env, so only the runner reads the file (one Keychain lookup).
*/
-dotenv.config({ path: path.resolve(__dirname, ".env.local"), quiet: true });
+if (!process.env.TEST_WORKER_INDEX) {
+ loadEnvFile({
+ path: path.resolve(__dirname, ".env.local"),
+ ignore: ["MISSING_ENV_FILE"],
+ quiet: true,
+ // Throw on a decryption failure instead of running every test against `encrypted:...` values
+ strict: true,
+ });
+}
// Use process.env.PORT by default and fallback to port 3000
const PORT = process.env.PORT ?? 3000;
@@ -27,8 +36,8 @@ const PORT = process.env.PORT ?? 3000;
// Set webServer.url and use.baseURL with the location of the WebServer respecting the correct set port
const BASE_URL = process.env.BASE_URL ?? `http://localhost:${PORT}`;
-// @ts-expect-error - NODE_ENV is not defined in the environment variables
-const isLocal = process.env.NODE_ENV === "local";
+// Local machine against the local dev server; a remote BASE_URL (Vercel preview) gets the CI timeouts
+const isLocal = !process.env.CI && !process.env.BASE_URL;
/**
* Analytics and error reporting are switched off for every project, and both of the app's
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c93c9c1..83cf6a0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -28,6 +28,9 @@ importers:
.:
dependencies:
+ '@dotenvx/dotenvx':
+ specifier: 2.34.2
+ version: 2.34.2
'@hookform/resolvers':
specifier: 5.9.1
@@ -233,9 +236,6 @@ importers:
autoprefixer:
specifier: 10.4.21
version: 10.4.21([email protected])
- dotenv:
- specifier: 17.2.3
- version: 17.2.3
eslint-plugin-playwright:
specifier: 2.11.0
@@ -459,6 +459,19 @@ packages:
resolution: {integrity: sha512-M1cyP6YstFQCjih54SAxCqHLMMi8QqV8tenpgGE48RTXWD7vfMYJiw/6xcCDpS2h28AcLpTsFCZA863Ge9yxzA==}
hasBin: true
+ '@dotenvx/[email protected]':
+ resolution: {integrity: sha512-vU83L7W77P4LPpzbkicEraleqEwh6a1mcyRvzZA4D/786mYAQH3yad7be86jx9voAuyWx1QhDcJ8h+Dzzt++ng==}
+ hasBin: true
+
+ '@dotenvx/[email protected]':
+ resolution: {integrity: sha512-M5GnIMsooWrk3BLgrAs53tqXxd7Rj0Ukc+JnjedLxlvxPkYsBFF/kKrfkpskrrvT/wwma5UdXq+wKHWBcjDe9g==}
+
+ '@dotenvx/[email protected]':
+ resolution: {integrity: sha512-tqePICOaMSZCpUXpTcooW8H+UMce1MTmMqI+nbeh1Xdh6Zgdl4ymkw+CZBQoCX8fcfTheCoi4PtUABUaqESp2A==}
+
+ '@dotenvx/[email protected]':
+ resolution: {integrity: sha512-XMAwZpxIajWaP31zLl2GQS3grIQsRY4B4VMss8fDv70XbGF/9aXSMHc3v9iJNZZVX/i0Loz1MAn6oM8dycbN4A==}
+
resolution: {integrity: sha512-t+iX+Wf5nRKyNzk8dviW3Ikb/280+aEJAnw9YXvCp2tYGPSkMki+NRY+8aNLmVFv3eNtMdvViPNOPxS8SZNP+w==}
engines: {bun: '>=1', deno: '>=2', node: '>=16'}
@@ -7159,6 +7172,10 @@ packages:
resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==}
+ resolution: {integrity: sha512-R+RODBqp6i2pPflGdq+xIOUkl+RNfGgHwoinecKu/JCuf2uO06cOKoDbI2P7Dn6KcswdKwrczbU6IYJ6K8X+wg==}
+ engines: {node: '>=18.17'}
+
resolution: {integrity: sha512-CLjCCLQ6UuMxWnbIylkisbRj31qxHPAurvena/0iwSVbQ2G1VY5/HjV0IRabOEbDHlzZlRdCrD4NhB0JtU40Pg==}
@@ -7435,6 +7452,10 @@ packages:
resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
+ resolution: {integrity: sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==}
+ engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0}
+
resolution: {integrity: sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==}
engines: {node: '>=10.0.0'}
@@ -7498,6 +7519,10 @@ packages:
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
engines: {node: '>=10'}
+ resolution: {integrity: sha512-DODGl1wJjA/s5pnJFKau9lIYHT81lnhob1i3e1TjxZRxEhWRKl74nTbWE6H5KlkViQQTo/Z29YFdxzTZAMY3ng==}
+ engines: {node: '>=18.19'}
+
resolution: {integrity: sha512-U/PBtDf35ff0D8X8D0jfdzHYEPFxAI7jJlxZXwCSez5M3190m+QobIfh+sWDWSHMCWWJN2AWamkegn6vr6YBTw==}
engines: {node: '>=18'}
@@ -7779,6 +7804,24 @@ snapshots:
picomatch: 4.0.7
which: 4.0.0
+ '@dotenvx/[email protected]':
+ dependencies:
+ '@dotenvx/primitives': 3.3.0
+ '@dotenvx/providers': 0.3.5
+ '@dotenvx/tooling': 1.0.5
+ undici: 6.29.0
+ write-file-atomic: 5.0.1
+ yocto-spinner: 1.2.2
+
+ '@dotenvx/[email protected]': {}
+
+ '@dotenvx/[email protected]':
+ dependencies:
+ '@dotenvx/primitives': 3.3.0
+ '@dotenvx/tooling': 1.0.5
+
+ '@dotenvx/[email protected]': {}
+
dependencies:
'@noble/ciphers': 1.3.0
@@ -14679,6 +14722,8 @@ snapshots:
+
dependencies:
base64-js: 1.5.1
@@ -15023,6 +15068,11 @@ snapshots:
+ dependencies:
+ imurmurhash: 0.1.4
+ signal-exit: 4.1.0
+
@@ -15070,6 +15120,10 @@ snapshots:
+ dependencies:
+ yoctocolors: 2.1.2
+