By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Why process.env.username readys the computer hostname (in Windows 10), instead of the key-value pair defined in config.env file
JavaScript

Javascript – Why process.env.username readys the computer hostname (in Windows 10), instead of the key-value pair defined in config.env file

Admin
Last updated: 2023/12/23 at 5:15 AM
Admin
Share
2 Min Read
Why process.env.username readys the computer hostname (in Windows 10), instead of the key-value pair defined in config.env file

Problem:

I defined username and password in config.env file and tried to retrieve with dotenv package.

Contents
Problem:Solution:

When I try to retrieve username, it gets the hostname. Regardless if I define all lowercase letters or uppercase letters; it returns the computer hostname.

Is it with the process.env.username having some variable name collision, and by design it would read the hostname for process.env.username?

Here is snippet code from app.js:

const express = require('express')
const app = express();

const dotenv = require('dotenv')

// Setting up the post for server
dotenv.config({
    path: './utils/config.env',
    override: true
})

console.log(`*** Username: ${process.env.USERNAME}`)
console.log(`*** Username: ${process.env.USER_NAME}`)
console.log(`*** Username: ${process.env.UNAME}`)
console.log(`*** Password: ${process.env.password}`)

const PORT = process.env.PORT;
app.listen(PORT, () => {
    console.log(`Server has started on port: ${process.env.PORT} in ${process.env.NODE_ENV} mode.`)
})

Here is what is inside config.env file:

PORT = 3000
NODE_ENV = development

USERNAME = USERNAME1
USER_NAME = USERNAME2
UNAME = USERNAME3
password = password@123

How should I understand this and (I guess) what is the recommended way of defining username and password in config.env file when we deal with NodeJS?

When I added ‘_’ (like USER_NAME, or any other form of ‘username’); then it works as expected.

Solution:

Dotenv by default will not overwrite existing shell variables.

You can configure dotenv with the override option to work around this.

require('dotenv').config({
  path: './utils/config.env',
  override: true,
});

This will ignore any existing environment variables with the same names as those in your config.env file.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article New to JavaScript, coming from Python, why is it not working? [duplicate] Javascript – New to JavaScript, coming from Python, why is it not working? [duplicate]
Next Article JavaScript - check if any radio button inputs = no [duplicate] Javascript – JavaScript – check if any radio button inputs = no [duplicate]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?