1- import dotenv from 'dotenv' ;
2- import path from 'path' ;
3- import fs from 'fs' ;
4- import { StringValue } from 'ms' ;
1+ import dotenv from "dotenv" ;
52
6-
7- // Possible locations for .env file
8- const possibleEnvPaths = [
9- path . resolve ( __dirname , '../../../.env' ) , // dev: src/
10- path . resolve ( __dirname , '../../../../.env' ) , // prod: dist/src/
11- ] ;
12-
13- // Find the first existing .env file
14- const envPath = possibleEnvPaths . find ( fs . existsSync ) ;
15-
16- console . log ( 'Loading environment from:' , envPath ) ;
17-
18- if ( ! envPath ) {
19- throw new Error ( 'Failed to find .env file in expected locations.' ) ;
20- }
21-
22- const result = dotenv . config ( { path : envPath } ) ;
23-
24- if ( result . error ) {
25- throw new Error ( 'Failed to load environment variables. Please check the .env file.' ) ;
3+ // Only load .env file in development; in production, Railway injects env vars automatically
4+ if ( process . env . NODE_ENV !== "production" ) {
5+ dotenv . config ( ) ;
266}
277
288export const env = {
299 node : {
30- env : process . env . NODE_ENV || ' development' ,
10+ env : process . env . NODE_ENV || " development" ,
3111 port : ( ( ) => {
32- const port = parseInt ( process . env . PORT || '' , 10 ) ;
12+ const port = parseInt ( process . env . PORT || "" , 10 ) ;
3313 return isNaN ( port ) ? 5000 : port ;
3414 } ) ( ) ,
35- host : process . env . HOST || ' 0.0.0.0' ,
15+ host : process . env . HOST || " 0.0.0.0" ,
3616 } ,
3717 jwt : {
38- secret : process . env . JWT_SECRET || 'default_secret' ,
39- refreshSecret : process . env . JWT_REFRESH_SECRET || 'refresh_default_secret' ,
40- expiresIn : process . env . JWT_EXPIRES_IN as StringValue || '1d' ,
41- refreshExpiresIn : process . env . JWT_REFRESH_EXPIRES_IN as StringValue || '30d' ,
18+ secret : process . env . JWT_SECRET || "default_secret" ,
19+ refreshSecret : process . env . JWT_REFRESH_SECRET || "refresh_default_secret" ,
20+ expiresIn : ( process . env . JWT_EXPIRES_IN as StringValue ) || "1d" ,
21+ refreshExpiresIn :
22+ ( process . env . JWT_REFRESH_EXPIRES_IN as StringValue ) || "30d" ,
4223 } ,
4324 ai : {
4425 geminiKey : process . env . GEMINI_API_KEY ,
@@ -53,14 +34,14 @@ export const env = {
5334 storageBucket : process . env . FIREBASE_STORAGE_BUCKET ,
5435 messagingSenderId : process . env . FIREBASE_MESSAGING_SENDER_ID ,
5536 appId : process . env . FIREBASE_APP_ID ,
56- measurementId : process . env . FIREBASE_MEASUREMENT_ID ,
57- databaseURL : process . env . FIREBASE_DATABASE_URL
37+ measurementId : process . env . MEASUREMENT_ID ,
38+ databaseURL : process . env . FIREBASE_DATABASE_URL ,
5839 } ,
5940 app : {
6041 frontend : process . env . FRONTEND_URL ,
6142 backend : process . env . BACKEND_URL ,
62- netlify : process . env . NETLIFY_URL
63- }
43+ netlify : process . env . NETLIFY_URL ,
44+ } ,
6445} as const ;
6546
6647export type Environment = typeof env ;
0 commit comments