Skip to content

Commit 4b6a9c7

Browse files
committed
fix: load timing issues w/ environment vars
1 parent e20ee2c commit 4b6a9c7

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

packages/express/src/app.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import { createExpressApp, initializeServices } from './app-factory.js';
2-
import logger from './logger.js';
3-
import ENV from './utils/env.js';
41
import dotenv from 'dotenv';
5-
import { initializeCouchDB } from './couchdb/index.js';
6-
import { initializeDataLayer } from '@vue-skuilder/db';
72

3+
// Load environment variables FIRST before importing any modules that depend on them
84
dotenv.config({
95
path:
106
process.argv && process.argv.length == 3
117
? process.argv[2]
128
: '.env.development',
139
});
1410

15-
// Now that dotenv is configured, we can validate the environment
11+
// Now import modules that depend on environment variables
12+
import { createExpressApp, initializeServices } from './app-factory.js';
13+
import logger from './logger.js';
14+
import ENV from './utils/env.js';
15+
import { initializeCouchDB } from './couchdb/index.js';
16+
import { initializeDataLayer } from '@vue-skuilder/db';
17+
18+
// Validate that environment variables are loaded
1619
const requiredVars = ['COUCHDB_SERVER', 'COUCHDB_PROTOCOL', 'COUCHDB_ADMIN', 'COUCHDB_PASSWORD', 'VERSION', 'NODE_ENV'];
1720
const missingVars = requiredVars.filter(v => !process.env[v]);
1821

packages/express/src/utils/env.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ function getVar(name: string): string {
2020
}
2121
}
2222

23+
// Use getter to read environment variables lazily after dotenv has loaded
2324
const env: Env = {
24-
COUCHDB_SERVER: getVar('COUCHDB_SERVER'),
25-
COUCHDB_PROTOCOL: getVar('COUCHDB_PROTOCOL'),
26-
COUCHDB_ADMIN: getVar('COUCHDB_ADMIN'),
27-
COUCHDB_PASSWORD: getVar('COUCHDB_PASSWORD'),
28-
VERSION: getVar('VERSION'),
29-
NODE_ENV: getVar('NODE_ENV'),
25+
get COUCHDB_SERVER() { return getVar('COUCHDB_SERVER'); },
26+
get COUCHDB_PROTOCOL() { return getVar('COUCHDB_PROTOCOL'); },
27+
get COUCHDB_ADMIN() { return getVar('COUCHDB_ADMIN'); },
28+
get COUCHDB_PASSWORD() { return getVar('COUCHDB_PASSWORD'); },
29+
get VERSION() { return getVar('VERSION'); },
30+
get NODE_ENV() { return getVar('NODE_ENV'); },
3031
};
3132

3233
export default env;

0 commit comments

Comments
 (0)