I've a problem. I'm new to nodejs so probably it's really easy to fix it. In the main directory, I've a .env
file. In the main.js file I've:
const express = require('express');
const app = express();
const http = require('http')
const {Pool, Client} = require('pg');
require("dotenv").config();
console.log(process.env.USER);
So, I simply want to print the USER
variabile that is located into my .env
file. Here is my .env
file:
USER=postgres
HOST=localhost
DATABASE=postgres
PASSWORD=mysecretpassword
PORT=5432
Anyway, when I write console.log(process.env.USER);
, the variable printed is not the USER
variable (that is postgres
), but my pc user account (that is John
). How is that possible?
CodePudding user response:
dotenv
add override
option at version 14.1.0 (2022-01-17) You can use override: true
option to
Override any environment variables that have already been set on your machine with values from your
.env
file.
require('dotenv').config({ override: true, debug: true })
console.log(process.env.USER);
Output:
[dotenv][DEBUG] "USER" is already defined in `process.env` and WAS overwritten
postgres
USER
is a system environment variable, see environ(7)
USER The name of the logged-in user (used by some BSD-derived programs). Set at login time, see section NOTES below.
CodePudding user response:
I don't have enough reputation to comment, but have you tried renaming your variables, adding a prefix like DB_
, so the variables names become DB_USER
, DB_HOST
etc.?
Maybe this variable is already taken by the process. And I personally don't like to overwrite them.
You can check running node
REPL in your terminal, then typing process.env
.
These are the Node environment variables that are already taken on my PC (running on Windows):
{
ALLUSERSPROFILE: 'C:\\ProgramData',
APPDATA: 'C:\\Users\\dizolan\\AppData\\Roaming',
ChocolateyInstall: 'C:\\ProgramData\\chocolatey',
CommonProgramFiles: 'C:\\Program Files\\Common Files',
'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
CommonProgramW6432: 'C:\\Program Files\\Common Files',
COMPUTERNAME: '...',
ComSpec: 'C:\\Windows\\system32\\cmd.exe',
DriverData: 'C:\\Windows\\System32\\Drivers\\DriverData',
FPS_BROWSER_APP_PROFILE_STRING: 'Internet Explorer',
FPS_BROWSER_USER_PROFILE_STRING: 'Default',
GOPATH: '...',
HOMEDRIVE: 'C:',
HOMEPATH: '...',
LOCALAPPDATA: '...',
LOGONSERVER: '...',
NUMBER_OF_PROCESSORS: '8',
OneDrive: '...',
OS: 'Windows_NT',
Path: '...',
PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW',
POWERSHELL_DISTRIBUTION_CHANNEL: 'MSI:Windows 10 Pro for Workstations',
PROCESSOR_ARCHITECTURE: 'AMD64',
PROCESSOR_IDENTIFIER: 'Intel64 Family 6 Model 142 Stepping 12, GenuineIntel',
PROCESSOR_LEVEL: '6',
PROCESSOR_REVISION: '8e0c',
ProgramData: 'C:\\ProgramData',
ProgramFiles: 'C:\\Program Files',
'ProgramFiles(x86)': 'C:\\Program Files (x86)',
ProgramW6432: 'C:\\Program Files',
PSModulePath: 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules',
PUBLIC: 'C:\\Users\\Public',
SESSIONNAME: 'Console',
SystemDrive: 'C:',
SystemRoot: 'C:\\Windows',
TEMP: '...',
TMP: '...',
USERDNSDOMAIN: '...',
USERDOMAIN: '...',
USERDOMAIN_ROAMINGPROFILE: '...',
USERNAME: '...',
USERPROFILE: '...',
windir: 'C:\\Windows'
}