FoalTS / foal

Full-featured Node.js framework 🚀
https://foalts.org/
MIT License
1.9k stars 142 forks source link

Config.ts bug? production.json not loading #587

Closed crossinghoods closed 4 years ago

crossinghoods commented 4 years ago

Not sure if anyone else is facing these 2 issues I'm having.

1) I believe there is a documentation error on this page https://foalts.gitbook.io/docs/topic-guides/deployment-and-environments/ship-to-production NODE_ENV=production npm run start shouldn't it be this instead? NODE_ENV=production

2) Trying to do a test deployment on my server but have found that production configuration does not load. I have created the .env file but FoalTS/config doesn't seem to automatically load it?

Instead I have put in a simple fix on my index.ts file to run the following function before running main()

// Load Env File
(async function loadEnv() {
    let path = ".env";
    if (!fs.existsSync(path)) {
        path = __dirname + "/.env";
    }

    if (fs.existsSync(path)) {
        const lines = fs.readFileSync(path).toString().split("\n");
        for (const l of lines) {
            const line = l.trim();
            if (line[0] === "#") {
                continue;
            }

            const [varName, value] = line.split("=");
            process.env[varName.trim()] = value.trim()
        }
    }
})().catch(err => { console.error(err); });
LoicPoullain commented 4 years ago

Hi @crossinghoods

  1. It is just an example on how to start the server with the env var NODE_ENV set to production.
  2. What OS are you on? Can you share the TS code that calls the Config class (using syntax highlighting) as well as your configuration file (yml, json or .env)? Thanks.
LoicPoullain commented 4 years ago

I'm closing old issues. Feel free to reopen it if you still need help on this.