Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS not recognizing .env file

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?

like image 626
pmiranda Avatar asked Apr 05 '20 05:04

pmiranda


People also ask

Where can I find process env file?

env files in your project root folder to set such environment variables. For that it appears to be using this package, which you might find useful also in other contexts.

Can not find module dotenv?

To solve the error "Cannot find module 'dotenv'", make sure to install the dotenv package by opening your terminal in your project's root directory and running the following command: npm install dotenv and restart your IDE and development server.

How do I read an environment variable in Node JS?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.

Why use env file or environment variables with Node JS?

Why use env file or environment variables with Node.js? Env files store the environment variables. It is NEVER recommended to hard code sensitive information like API keys. Suppose you created a TODO application which stores data in MongoDB and hardcoded the connection string (they are secure as passwords).

What is custom-ENV in Node JS?

That is what custom-env has come to do. Custom env is a library built to make development easier by allowing multiple .env configuration for different environments. This is done by loading environment variables from a .env.envname file into the node’s process.env object.

How to check if Node JS is installed or not?

In the variable value option inside that dialogue box, paste the complete path of the location where node.js is installed in your system.Then click on OK. Restart the command prompt again and now verify by typing node-v in the command prompt. It will now display the version of the node which you’ve installed from the internet .

What is dotenv in Node JS?

The dotenv package enables loading of a .env file in a Node.js project, which serves as a central place to manage environment variables. This single file approach makes updating and maintaining environment variables easy. If you are new to environment variables, read this article first Environment variables in Node.js


Video Answer


1 Answers

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

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

require('dotenv').config()

like image 155
JBallin Avatar answered Sep 27 '22 23:09

JBallin