Skip to content

[Bug]: CLI commands cant use .env.staging or .env.production #10679

Description

@leobenzol

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.1.2",
    "@medusajs/cli": "2.1.2",
    "@medusajs/framework": "2.1.2",
    "@medusajs/medusa": "2.1.2",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.1.2",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.12.0

Database and its version

Postgres 17.2

Operating system name and version

WSL 2 Ubuntu 24

Browser name

No response

What happended?

As per the title. Right now in staging/production I'm forced to have DATABASE_URL also in an otherwise empty .env file

I wouldnt bother to report it right now if it wasnt such a simple fix.
Should be as easy as, inside packages/cli/medusa-cli/cli.js, using loadEnv from @medusajs/utils instead of require("dotenv).config() directly

Expected behavior

.env.staging/production should work with medusa cli, specifically npx medusa db:setup should work

Actual behavior

.env.staging/production dont work with medusa cli, specifically npx medusa db:setup give the error:

error: Missing "DATABASE_URL" inside the .env file. The value is required to connect to the PostgreSQL server

Link to reproduction repo

https://github.com/medusajs/medusa-starter-default

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions