NodeJS not recognizing .env file

15,404

Solution 1

Seems like you need to require/configure dotenv. Docs:

As early as possible in your application, require and configure dotenv.

require('dotenv').config()

Solution 2

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

In the file you are using environment variables, As early as possible, require the "dotenv" and in the config() method, specify the path of the .env file, even if it in your root directory or the same directory where node starts.

The code for requiring and specifying file in the same directory is in the first line in the answer.

Also, for further reading 📖 , you can visit https://github.com/motdotla/dotenv#path

Solution 3

To further expand on @JBallin answer

you should use this on your app.js

  • Or if that does not work then you will need to explicitly add it to the file you are wanting to use those Variables

Sharing image, as its sometimes easier to see expanded enter image description here

code here =>

require('dotenv/config') // require the dotenv/config at beginning of file
const express = require('express')
const mongoose = require('mongoose')
Share:
15,404
pmiranda
Author by

pmiranda

Pretty old school in some stuff (I used to own at Logo with Poly 5 with colors and making some races with them, while my old brother was ruling with Frogger, Choplifter and Dig-dug at our Atari 130XE) and newbie in others. I work (or used to in some cases) with jQuery, ReactJS 15/16/17 (FLUX, Redux, Hooks), Symfony 2-3, OpenLayers 2-3, ArcGIS JS-API 3, PostGIS, Laravel 5.1, 5.4, 5.5, 5.6, 5.8, Angular 6-7, AngularJS, NodeJS, Express, UIKit 2, Bootstrap 2,3,4, Material-UI, React Native, Lerna, Serverless framework, AWS (Cognito, DynamoDB, Lambdas, API Gateway, etc), Github Actions, CircleCI, now I'm struggling to work with MongoDB, Drone, Sonarqube. I also know a lot of nutrition (the sumo pic is not me by the way). Actually, I think I know more of nutrition than anything that I post or ask here at StackOverflow.

Updated on June 23, 2022

Comments

  • pmiranda
    pmiranda almost 2 years

    I have like 5 NodeJS services running, but I have a problem in one of those.

    This is the nodemon.json file:

    {
      "watch": ["**/*.ts"],
      "ext": "ts,json",
      "ignore": ["./test/*.ts"],
      "exec": "node -r ts-node/register -r dotenv/config Index.ts dotenv_config_path=$(pwd)/.env",
      "env": {
        "NODE_ENV": "development"
      }
    }
    

    It's the same as the rest of services. When I run npm run dev I got error messages depending on which value is taking from the .env file, example:

    const LOCAL_CONFIGURATION = {
        PORT_APP: 8082,
        MONGODB: {
            SERVER: process.env.MONGO_DTE,
            AUTH: {
                auth: {
                    password:process.env.MONGO_PASSWORD,
                    user:process.env.MONGO_USER
                }
            },
        },
        MS_NOTIFICACION: "http://localhost:8089/notificacion",
        ELASTIC_PATH: process.env.ELASTIC_PATH,
        ...COMMON,
    };
    

    The first error message is: ConfigurationError: Missing node(s) option That message is produced because it's not reading the value from process.env.ELASTIC_PATH, but if I put a hardcoed value like "http://with.the.correct.url" and it tries again to run, I get another error:

    Error: Credentials must be provided when creating a service client That error is because it's trying to read password:process.env.MONGO_PASSWORD and user:process.env.MONGO_USER

    etc, so, there's a problem on reading the .env file. I know that .env file has those values, and the file is in UTF-8, without quotes, etc. The .env file is the same file as the other services, it works ok in the rest but I don't know why is not getting read here.

    Any idea?

    EDIT:

    enter image description here

    Plus, I put a console.log(process.env); in config.ts file and it shows values like this:

    enter image description here

    But there's no values from the .env for example, there in the picture there's a value called COMPUTERNAME so if I put console.log(process.env.COMPUTERNAME); I get: IBM-NOT87

    Why is not getting the .env file?