16

I have a problem with the dotenv package.

My application folder:

 |_app_folder
      |_app.js
      |_password.env
      |_package.json

I've of course install dotenv, but when i tried to log a process.env variables, the result is always undefined, please can you help me ?

password.env :

//password.env 
CLIENT_ID=xxxxxxxx

app.js :

//app.js
const express = require('express');
const app = express();
const Twig = require("twig");

//Require dotenv
require('dotenv').config();

// Setting the Twig options
app.set("twig options", {
    allow_async: true, 
    strict_variables: false
});

app.get('/', function (req, res) {
  //Trying to log it
  console.log(process.env.CLIENT_ID);
  //
  res.render('index.twig', {
    date : new Date().toString()
  });
});

app.get('/instagram',function(req,res){
  // Building the URL
  let url = 'https://api.instagram.com/oauth/authorize/?client_id=';
  // Redirect to instagram for oauth 
  res.redirect(url);
})

app.listen(3000, function () {
  console.log('Running');
})

Thank you for your time.

6 Answers 6

15

By default the dotenv package does only load a file named .env if you want to load another file you need to specify the path

require("dotenv").config({ path: "path/to/file" })

Resources:

https://www.npmjs.com/package/dotenv

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks you i didn't know that, i replace the require with this one : require("dotenv").config(path.join(__dirname,'password.env')); but it still don't work
Try to log the path you are creating and check if it is correct
i've did and it's the good one : /home/thomas/instagram-oauth-login/password.env
No problem, I messed that up! Sorry
As a complement I had a weird case where the .env file is not loaded either, and I had to add require("dotenv").config({path:".env"})
4

When using import instead of require. -

You can use -r (require) to preload dotenv. You do not need to require and load dotenv in your application code.

 $ node -r dotenv/config app.js

Comments

4

I was having somewhat the same problem for a while turns out you just have to put the .env file in the root of the directory (top-most level).

I know this post is old but I just want to make sure no one struggles with such a simple task again.

Comments

2

Even though I put the .env file in the root folder, still console.log(process.env.myKey) is undefined. The fix worked for me is I put the path to the env file in the require config itself like below. (It's in the root of the file - so "./.env)

require("dotenv").config({path:"./.env"})

Comments

2

Another important note:

Place your .env file in the root folder, not in /src

Comments

0

Take into account (as in my case) that if the .env file is not UTF8 encoded like UTF16 in my case, you will always get undefined (Windows OS)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.