0

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

2
  • 1
    .env file does not get automatically loaded. You need to use some package in order to pass those to node. Commented Jun 25, 2021 at 7:21
  • okay thanks. Where should I add that code? inside my database file? Commented Jun 25, 2021 at 7:22

2 Answers 2

2

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

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

5 Comments

Amit can you clairfy a bit more about this? Since I got this code from client and I am not allowed to change the code. can we access it without importing dotenv? or if its not possible where should I import dotenv and write dotenv.config()
you have to import it in this file or any file executed before this. if you dont want to change code then you can define environment variables in your OS
. I tried to import env package inside database file, now it shows following error #8 1.410 Error while running command build: Could not load js config file /config/database.js: Cannot use import statement outside a module
maybe you are using older node change to import dotenv from "dotenv"; dotenv.config()
I have tried everything regarding the dotenv and its process.env syntax, it shows error Error connecting to the Mongo database. URI does not have hostname, domain name and tld error. no idea why :/
1

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.

3 Comments

Thanks for the detailed answer. I tried to import env package inside database file, now it shows following error #8 1.410 Error while running command build: Could not load js config file /config/database.js: Cannot use import statement outside a module
It's all about how you use JS then - most often node works with CommonJS (const hi = require("hi"). Import statements are ES6 modules, and those two can hardly live together :) If you're following Amit's code, then you should use const dotenv = require("dotenv")
I have tried the above code and added npm install dotenv in my docker file as well, but it still shows the " Error connecting to the Mongo database. URI does not have hostname, domain name and tld" error. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.