Skip to content

Commit

Permalink
fix: Early dotenv
Browse files Browse the repository at this point in the history
We need to call `dotenv.config()` before other files are imported to
have the .env configuration take effect in NODE_ENV=production.
  • Loading branch information
pettermachado committed Oct 25, 2024
1 parent 594e760 commit 5408808
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dotenv from "dotenv";
import { getLogger } from "./logger/index.js";

const logger = getLogger("lib/env");
logger.info("Starting in NODE_ENV=" + process.env.NODE_ENV);

// The dev server parses keys in non-production
if (process.env.NODE_ENV === "production") {
const config = dotenv.config();
logger.info(
"Parsed .env file with keys: " + Object.keys(config.parsed ?? {}).join(","),
);
}
13 changes: 2 additions & 11 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import "./lib/env.js";
import { getLogger } from "./lib/logger/index.js";
import { createRequestHandler } from "@remix-run/express";
import type { ServerBuild } from "@remix-run/node";
import express from "express";
import dotenv from "dotenv";
import pinoHttp from "pino-http";

import { sync } from "./lib/db/index.js";
import worker from "./lib/worker/index.js";
import apiRouter from "./api/index.js";
import { getLogger } from "./lib/logger/index.js";

const logger = getLogger("server");
logger.info("Starting in NODE_ENV=" + process.env.NODE_ENV);

// The dev server parses keys in non-production
if (process.env.NODE_ENV === "production") {
const config = dotenv.config();
logger.info(
"Parsed .env file with keys: " + Object.keys(config.parsed ?? {}).join(","),
);
}

const app = express();
if (process.env.REQUEST_LOG) {
Expand Down

0 comments on commit 5408808

Please sign in to comment.