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

Faisal Shani
  • 698
  • 1
  • 13
  • 37

2 Answers2

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

Amit
  • 3,662
  • 2
  • 26
  • 34
  • 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() – Faisal Shani Jun 25 '21 at 07:21
  • 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 – Amit Jun 25 '21 at 07:23
  • . 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 – Faisal Shani Jun 25 '21 at 07:39
  • maybe you are using older node change to import dotenv from "dotenv"; dotenv.config() – Amit Jun 25 '21 at 15:08
  • 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 :/ – Faisal Shani Jun 25 '21 at 15:55
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.

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • 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 – Faisal Shani Jun 25 '21 at 07:39
  • 1
    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")` – Andrey Popov Jun 25 '21 at 07:48
  • 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. :/ – Faisal Shani Jun 25 '21 at 08:30