Skip to content

Commit 4c7553f

Browse files
authored
fix: replace dotenv devDependency with inline env parser in doctor (#61)
dotenv was listed as a devDependency but imported at runtime, causing ERR_MODULE_NOT_FOUND when running `workos doctor` via npx.
1 parent cc590b0 commit 4c7553f

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/doctor/checks/environment.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import { readFileSync, existsSync } from 'node:fs';
22
import { join } from 'node:path';
3-
import { parse as parseDotenv } from 'dotenv';
43
import type { EnvironmentInfo, EnvironmentCheckResult, DoctorOptions } from '../types.js';
54

5+
function parseEnvFile(content: string): Record<string, string> {
6+
const result: Record<string, string> = {};
7+
8+
for (const line of content.split('\n')) {
9+
const trimmed = line.trim();
10+
if (!trimmed || trimmed.startsWith('#')) continue;
11+
12+
// Strip optional `export ` prefix
13+
const entry = trimmed.startsWith('export ') ? trimmed.slice(7) : trimmed;
14+
const eqIndex = entry.indexOf('=');
15+
if (eqIndex === -1) continue;
16+
17+
const key = entry.slice(0, eqIndex).trim();
18+
let value = entry.slice(eqIndex + 1).trim();
19+
20+
// Remove surrounding quotes (single or double)
21+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
22+
value = value.slice(1, -1);
23+
}
24+
25+
result[key] = value;
26+
}
27+
28+
return result;
29+
}
30+
631
/**
732
* Load environment variables from project's .env files.
833
* Priority: .env.local > .env (matching Next.js/Vite conventions)
9-
* Uses dotenv parser for proper handling of quotes, multiline, exports, etc.
1034
*/
1135
function loadProjectEnv(installDir: string): Record<string, string> {
1236
const env: Record<string, string> = {};
@@ -19,8 +43,7 @@ function loadProjectEnv(installDir: string): Record<string, string> {
1943
if (existsSync(filePath)) {
2044
try {
2145
const content = readFileSync(filePath, 'utf-8');
22-
const parsed = parseDotenv(content);
23-
Object.assign(env, parsed);
46+
Object.assign(env, parseEnvFile(content));
2447
} catch {
2548
// Ignore read errors
2649
}

0 commit comments

Comments
 (0)