help icon indicating copy to clipboard operation
help copied to clipboard

Subprocess launched when calling JS function from different file (process.env)?

Open Creative-Difficulty opened this issue 3 years ago • 0 comments

Details

I currently have circa 400-500 lines of NodeJS code in one file for my Project and I want to split it up into separate smaller (lib) files.

I also want to put the function to check the process.env properties with the dotenv package (https://www.npmjs.com/package/dotenv). Can I check the process.env of the main.js file in exaample.js? See files below! Please forgive any typos! Regards, Alex

Node.js version

v17.7.2

Example code

File 1 (main file):

import function2 from "example.js"

function function1() {
    var result = await function2();
    console.log(result)
}

example.js:

import log4js from "log4js"
import dotenv from "dotenv";

const logger = log4js.getLogger();
dotenv.config();

export default async function checkENV() {
    return new Promise((resolve, reject) => {
        if(/\s/.test(process.env.URI)) {
            logger.warn("The environment variable URI contains a whitespace, defaulting to none");
            process.env.URI = ""
        }
        
        if(process.env.LOGLEVEL === "" || process.env.LOGLEVEL !== "info" || process.env.LOGLEVEL !== "production" || process.env.LOGLEVEL !== "debug" || process.env.LOGLEVEL !== " info" || process.env.LOGLEVEL !== "info ") {
            logger.level = "info";
            logger.warn("The environment variable LOGLEVEL isnt set or isnt properly set, defaulting to info");
        } else {
            logger.level = process.env.LOGLEVEL;
        }
        
        if(process.env.PORT === ""|| /\s/.test(process.env.PORT)) {
            logger.warn("The environment variable PORT isnt set or isnt properly set, defaulting to 8082")
            process.env.PORT = 8082
        }
    })
}

The .env File:

PORT = 

LOGLEVEL=

URI = 

Operating system

macOS Monterey

Scope

code

Module and version

dotenv

Creative-Difficulty avatar Jun 20 '22 15:06 Creative-Difficulty