Faisal Shani
Faisal Shani

Reputation: 810

Cant access variables from .env file in node.js application

I am trying to dockerize a strapi app with mongodb atlas database. The issue I am facing is that database file in /config is not reading the variable from .env file.

.env file

HOST=0.0.0.0
PORT=1337
DATABASE_HOST=xyz.mongodb.net
DATABASE_USERNAME=abc-admin
DATABASE_PASSWORD=12345xyz
ADMIN_JWT_SECRET=abcd1234

Database connection code

const {
  DATABASE_HOST,
  DATABASE_USERNAME,
  DATABASE_PASSWORD
} = process.env;

module.exports = ({ env }) =>
  ({
    defaultConnection: 'default',
    connections: {
      default: {
        connector: 'mongoose',
        settings: {
          host: env('DATABASE_HOST', process.env.DATABASE_HOST),
          srv: env.bool('DATABASE_SRV', true),
          port: env.int('DATABASE_PORT', 27017),
          database: env('DATABASE_NAME', 'xyz-dev'),
          username: env('DATABASE_USERNAME', process.env.DATABASE_USERNAME),
          password: env('DATABASE_PASSWORD', process.env.DATABASE_PASSWORD)
        },
        options: {
          authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
          ssl: env.bool('DATABASE_SSL', true),
        },
      },
    },
  });

I have tried with process.env and without it in the above file. But when I run the image after build it shows below error

error Error connecting to the Mongo database. URI does not have hostname, domain name and tld

Any idea what I am doing wrong here? Thanks

Upvotes: 0

Views: 3605

Answers (2)

Andrey Popov
Andrey Popov

Reputation: 7510

Here's a bit more elaborate answer to your question (after reading your comments). Creating .env file means you just created it. It doesn't get automatically loaded. It's a typical way to use on unix machines, but has no relation to Node whatsoever.

What you need to do is somehow parse the content of that file (which is purely text), convert it to key-value pairs and pass it to node. There are many packages, and one that Amit showed is dotenv. It does all the work for you, and at the end, you get your variables injected inside process.env.

The simplest way would be to install this package (from npm) and use it as described. But if you cannot modify the code in any way, then you can simply parse the content of the file with a script, and then start the node server. Here's an example (taken from npm scripts: read .env file):

"scripts": {
  "example": "some-lib --argument --domain $(grep DOMAIN .env | cut -d '=' -f2)"
}

The drawback here is that it doesn't work across various operating systems and that using a specific library for that is way more tested than your manual scripts.

Upvotes: 1

Amit
Amit

Reputation: 4290

One option is to use dotenv you need to import dotenv and run dotenv.config() before you can start using env variables

so change to

import dotenv from "dotenv";
dotenv.config()
// your code which user process.env

other option is to define all those env variable on your OS level. On unix you can add to ~/.bashrc file

Upvotes: 2

Related Questions