@@ -10,26 +10,52 @@ interface EnvConfig {
1010}
1111
1212function parseEnv ( ) : EnvConfig {
13- const localenv = path . resolve ( __dirname , '../../.env' ) ;
14- const prodenv = path . resolve ( __dirname , '../../.env.prod' ) ;
15-
16- const envFiles = [
17- { path : prodenv , isProd : true } ,
18- { path : localenv , isProd : false }
13+ // Try multiple possible paths for environment files
14+ const possibleBasePaths = [
15+ path . resolve ( __dirname , '../../' ) , // From config/lib to project root
16+ path . resolve ( __dirname , '../../../' ) , // From node_modules to project root
17+ path . resolve ( __dirname , '../' ) , // From client directory to project root
18+ process . cwd ( ) , // Current working directory
19+ path . resolve ( process . cwd ( ) , '../' ) , // From client directory to project root
1920 ] ;
20-
21- const availableEnvFiles = envFiles . filter ( ( { path : envPath } ) => fs . existsSync ( envPath ) ) ;
2221
23- if ( availableEnvFiles . length === 0 ) {
22+ let foundEnvFiles : Array < { path : string , isProd : boolean } > = [ ] ;
23+
24+ // Search for .env files in all possible locations
25+ for ( const basePath of possibleBasePaths ) {
26+ const localenv = path . join ( basePath , '.env' ) ;
27+ const prodenv = path . join ( basePath , '.env.prod' ) ;
28+
29+ const envFiles = [
30+ { path : prodenv , isProd : true } ,
31+ { path : localenv , isProd : false }
32+ ] ;
33+
34+ const availableEnvFiles = envFiles . filter ( ( { path : envPath } ) => fs . existsSync ( envPath ) ) ;
35+
36+ if ( availableEnvFiles . length > 0 ) {
37+ foundEnvFiles = availableEnvFiles ;
38+ break ; // Use the first location where we find env files
39+ }
40+ }
41+
42+ if ( foundEnvFiles . length === 0 ) {
43+ // Show all possible paths in error message for debugging
44+ const allPossiblePaths : string [ ] = [ ] ;
45+ for ( const basePath of possibleBasePaths ) {
46+ allPossiblePaths . push ( path . join ( basePath , '.env' ) ) ;
47+ allPossiblePaths . push ( path . join ( basePath , '.env.prod' ) ) ;
48+ }
49+
2450 throw new Error (
25- `No environment file found. Expected one of :\n` +
26- `- ${ localenv } \n` +
27- `- ${ prodenv } `
51+ `No environment file found. Searched in these locations :\n` +
52+ allPossiblePaths . map ( ( p : string ) => `- ${ p } ` ) . join ( '\n' ) +
53+ `\n\nPlease create a .env file in your project root directory. `
2854 ) ;
2955 }
3056
31- const file = availableEnvFiles . find ( ( { isProd : prod } ) => isProd === prod ) ?. path ||
32- availableEnvFiles [ 0 ] . path ;
57+ const file = foundEnvFiles . find ( ( { isProd : prod } ) => isProd === prod ) ?. path ||
58+ foundEnvFiles [ 0 ] . path ;
3359 try {
3460 const envContent = fs . readFileSync ( file , 'utf8' ) ;
3561 const config = dotenv . parse ( envContent ) ;
0 commit comments