Skip to content

[Bug]: Production server fails to start due to environment variables not being loaded #112

@ihsankahveci

Description

@ihsankahveci

Pre-flight checklist

  • I have searched the existing issues
  • I have read the contributing guidelines

[Bug]: Production server fails to start due to environment variables not being loaded

Describe the bug

When running the server with the production command (npm start), the application crashes immediately. The error message Error: Parameter 'sid' is not valid. from the Twilio library indicates that environment variables from the .env file are not being loaded. This issue does not occur when using the development command (npm run dev), which uses tsx to explicitly load the .env file. This suggests a race condition or misconfiguration in how the production build loads dotenv.

Steps to reproduce

  1. Navigate to the server directory.
  2. Create a valid .env file by copying .env.example and filling in the required variables (at minimum MONGODB_URI and JWT_SECRET).
  3. Install dependencies with npm install.
  4. If a build directory doesn't exist, create one with npm run build.
  5. Attempt to start the server using the production command: npm start.
  6. Observe the application crash with the error shown in the logs.

Expected behavior

The server should start up successfully on the configured port (e.g., 1234), connect to the database, and initialize all services without crashing. The environment variables from the .env file should be loaded and available to the application at runtime, just as they are with npm run dev.

Relevant log output / screenshots

> [email protected] start
> node build/index.js

/Users/ihsankahveci/respondent-driven-sampling/server/node_modules/twilio/lib/rest/verify/v2/service.js:36
            throw new Error("Parameter 'sid' is not valid.");
                  ^

Error: Parameter 'sid' is not valid.
    at new ServiceContextImpl (/Users/ihsankahveci/respondent-driven-sampling/server/node_modules/twilio/lib/rest/verify/v2/service.js:36:19)
    at instance.get (/Users/ihsankahveci/respondent-driven-sampling/server/node_modules/twilio/lib/rest/verify/v2/service.js:315:16)
    at V2.instance [as _services] (/Users/ihsankahveci/respondent-driven-sampling/server/node_modules/twilio/lib/rest/verify/v2/service.js:313:41)
    at file:///Users/ihsankahveci/respondent-driven-sampling/server/build/routes/v1/auth.js:15:40
    at ModuleJob.run (node:internal/modules/esm/module_job:377:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:671:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.11.1

Software version

v1 - 527f8969a37bc8aeb6b92759650caae139ece6bb

Environment

macOS

None

Other environment details

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions