Reputation: 369
I'm using dotenv v8.2.0 in my NestJS project, and it has always worked in my production environment. I cloned the project on my new pc, made a .env file with the right variables in the root folder, but the .env file now doesn't get built into the dist folder.
The .env file:
databaseHost=database-di...
databasePassword=a3^U...
The setup of my main.ts file (only relevant parts):
import { config } from 'dotenv';
import * as path from 'path';`
const ENV_FILE = path.join(__dirname, '..', '.env');
config({ path: ENV_FILE });
When I log the dotenv config function, i get the following:
{
error: Error: ENOENT: no such file or directory, open 'C:\Users\Jasper\***\dist\.env'
at Object.openSync (fs.js:461:3)
at Object.readFileSync (fs.js:364:35)
at Object.config (C:\Users\Jasper\***\node_modules\dotenv\lib\main.js:96:29)
at Object.<anonymous> (C:\Users\Jasper\***\dist\src\main.js:21:22)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
at Module.load (internal/modules/cjs/loader.js:1040:32)
at Function.Module._load (internal/modules/cjs/loader.js:929:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: 'C:\\Users\\Jasper\\***\\dist\\.env'
}
When I look at the dist folder, the .env file does indeed not get ported over, while it used to always work. Logging the variables in the .env folder returns undefined
.
Does anyone know what I'm doing wrong?
Upvotes: 7
Views: 8383
Reputation: 70111
A .env
file is not a Typescript or JavaScript file, and as such will not be moved by Typescript. You may be able to move it using the assets
property of the nest-cli.json
, but then you'll need to make sure to commit your .env
file which is a bad practice. Instead, the .env
file should be read from the project root (same level as package.json
) and each environment should have its own .env
file to keep the secrets secured.
Upvotes: 8