Ubuntu: Problems Loading Environmental Variables in Ubuntu Environments

Asked 1 years ago, Updated 1 years ago, 40 views

Hello.

Windows is developing nodejs and trying to deploy to Ubuntu. in the development environment.Read environment variables well from env file. Only undefined appears in ubuntu production.

When the library is required, dotenvpm usage is written as follows:

require('dotenv').config()

Is it not possible to load the library because there is an option that I don't know when I request it? If you need a folder structure or other code, could you tell me?

Thank you for reading.

ubuntu node.js env

2022-09-22 18:06

2 Answers

#  * .env
a=1
b=2

I tested it in mint (based on ubuntu 18.04), and there is no problem.

let dotenv = require('dotenv');
require('dotenv').config({ path: '/home/allinux/.env' });

console.log(process.env.a);
console.log(process.env.b);


1
2


2022-09-22 18:06

The directory structure is as follows:

└─src
    ├─db
    │  │  └─models
    ├─routes
    │  │  ├─blog
    │  │  ├─blogFeed
    │  │  ├─youtube
    │  │  └─youtubeFeed
    └─utils

src/app.js loads environment variables without any settings. The problem is that I wrote the db loader in src/db/models/index.js as per sequelize code convention. The code below is src/db/models/index.js:

require('dotenv').config();
const mongoose = require('mongoose');
const env = process.env;

module.exports = () => {
  connect = () => {
    mongoose.connect(
      `${env.MONGO_URI}${env.MONGO_DATABASE}`,
      {
        useNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: true
      },
      err => {
        if (err) {
          console.error(`connection error:`, err);
        }
        console.log(
          `> Connected on
 - - URI: ${env.MONGO_URI}
 - - Database: ${env.MONGO_DATABASE.toUpperCase()} 
 - - Environment: ${env.NODE_ENV.toUpperCase()}`
        );
      }
    );
  };
  connect();
  mongoose.connection.on('disconnected', connect);
};

It's obviously working well done locally. undefined does not set the environment variable. Heroku of course set environmental variables on the setting page, so .It works well in production mode regardless of env file.

The problem is .envI must have entered the Ubuntu instance by file The environment variable set in the env file is set to undefined.

dotenvI looked it up in the GitHub repository or StackOverflow, but all I asked was to write path. I thought this would fit, so I tried to copy it, but it didn't work.

// local env
let path = require('path').join(__dirname, '../../../.env');
require('dotenv').config({
  path: path
});

console.log(path); // C:\dev\training\rss-feed\.env

In Ubuntu,

// ubuntu instance env
let path = require('path').join(__dirname, '../../../.env');
require('dotenv').config({
  path: path
});

console.log(path); // /home/ubuntu/training-rss-feed/.env

The path is taken on the console, but I don't understand why environmental variables are not recognized. I'd appreciate it if you could let me know if I made a stupid mistake.


2022-09-22 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.