medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Running Medusa on Google Cloud Compute Engine

Open faridghar opened this issue 1 year ago • 11 comments

Bug report

Describe the bug

Unable to log into Medusa admin dashboard with message "These credentials do not match our records.". The credentials are correct though. In Chrome console, I'm seeing a bunch of http://localhost:9000/admin/batch-jobs?limit=100 net::ERR_CONNECTION_REFUSED errors. I suspect this is because the Medusa backend being pointed to is localhost:9000. I've tried to change this by setting the environment variable MEDUSA_ADMIN_BACKEND_URL=http://<VM_EXTERNAL_IP> but this seems to just be ignored.

System information

Medusa version (including plugins): ├── @babel/[email protected] ├── @babel/[email protected] ├── @babel/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @medusajs/[email protected] ├── @tanstack/[email protected] ├── @types/[email protected] ├── @types/[email protected] ├── @types/[email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected]

Node.js version: v20.9.0 Database: Postgres Operating system: Debian Browser (if relevant): Chrome

Steps to reproduce the behavior

  1. Run npx create-medusa-app@latest --seed --with-nextjs-starter on Google Cloud Compute Engine VM
  2. Log in with default Admin credentials ([email protected], password=supersecret)

Expected behavior

Login is successful using default credentials

Screenshot 2023-11-13 09 29 02

faridghar avatar Nov 13 '23 05:11 faridghar

I am also having this problem running the Medusa admin on a remote machine.

The admin dashboard will use my remote machine's address when I run it in production mode using "yarn start", but I can't seem to get it to respect any URL other than "localhost:9000" in development mode

norrisduncan avatar Nov 15 '23 05:11 norrisduncan

I bet this clue has something to do with it. I believe this would be in node_modules/@medusajs/admin-ui/vite.config.dev.ts in your project structure, but I found it on GitHub at https://github.com/medusajs/medusa/blob/master/packages/admin-ui/vite.config.dev.ts

Screenshot_20231115-010409~2

norrisduncan avatar Nov 15 '23 06:11 norrisduncan

This is not just a Google Cloud issue then. How are we the only people running into this issue?

faridghar avatar Nov 16 '23 16:11 faridghar

This is not just a Google Cloud issue then. How are we the only people running into this issue?

I suspect it's because 1. it only happens when serving the medusa admin in dev mode, 2. it only causes problems when you're trying to access the admin dev server from outside of localhost, and maybe 3. other people are smarter than us and have figured it out on their own

norrisduncan avatar Nov 18 '23 03:11 norrisduncan

I think I was able to figure it out... Something is overwriting process.env.MEDUSA_BACKEND_URL with "http://localhost:9000", no matter what I do.

I should have written down my steps, but I think this should do it, if you are starting from the most recent "npm create-medusa-app@latest" output, as I am, I just ran the command a few days ago.

I think all I had to change was the contents of "node_modules/@medusajs/admin-ui/ui/src/constants/medusa-backend-url.ts", and replace the line

export const MEDUSA_BACKEND_URL = process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"

with

export const MEDUSA_BACKEND_URL = "http://yourMedusaBackendUrlHere:yourBackendPortHere"

replacing "yourMedusaBackendUrlHere:yourBackendPortHere" in the string with the url and port of your running backend server.

Then I think I was able to serve admin and it would point to my backend correctly, after setting CORS correctly.

There may be something going on with the medusa-admin CLI setting the backend to "localhost:9000" by default and that's overwriting my process.env variables, but I'm not sure.

If that doesn't do it for you I have a couple more things I tried that might have done it, I should have been keeping track better.

This also doesn't account for a separate issue which is that "npm create-medusa-app@latest" no longer populates the db with the default admin user "[email protected]", who will have to be added separately.

norrisduncan avatar Nov 18 '23 04:11 norrisduncan

Still no luck. Could you dump the following files for me please:

  1. .env
  2. .medusa-config.js
  3. node_modules/@medusajs/admin-ui/ui/src/constants/medusa-backend-url.ts

as well as any environment variables you explicitly export?

faridghar avatar Nov 18 '23 15:11 faridghar

Here you go:

.env relevant lines

STORE_CORS=http://[[myDomainName]]:8000,http://[[myDomainName]]:7001
ADMIN_CORS=http://[[myDomainName]]:7000,http://[[myDomainName]]:7001
MEDUSA_BACKEND_URL=http://[[myDomainName]]
COOKIE_SECRET=supersecret
JWT_SECRET=supersecret

My whole medusa-config.js:

const dotenv = require("dotenv");

let ENV_FILE_NAME = "";
switch (process.env.NODE_ENV) {
  case "production":
    ENV_FILE_NAME = ".env.production";
    break;
  case "staging":
    ENV_FILE_NAME = ".env.staging";
    break;
  case "test":
    ENV_FILE_NAME = ".env.test";
    break;
  case "development":
  default:
    ENV_FILE_NAME = ".env";
    break;
}

try {
  dotenv.config({ path: process.cwd() + "/" + ENV_FILE_NAME });
} catch (e) {}

// CORS when consuming Medusa from admin
const ADMIN_CORS =
  process.env.ADMIN_CORS || "http://localhost:7000,http://localhost:7001";

// CORS to avoid issues when consuming Medusa from a client
const STORE_CORS = process.env.STORE_CORS || "http://localhost:8000";

const DATABASE_URL =
  process.env.DATABASE_URL || "postgres://localhost/medusa-starter-default";

const REDIS_URL = process.env.REDIS_URL || "redis://localhost:6379";

const plugins = [
  `medusa-fulfillment-manual`,
  `medusa-payment-manual`,
  {
    resolve: `@medusajs/file-local`,
    options: {
      upload_dir: "uploads",
    },
  },
  {
    resolve: "@medusajs/admin",
    /** @type {import('@medusajs/admin').PluginOptions} */
    options: {
      serve: true,
      autoRebuild: true,
      develop: {
        open: process.env.OPEN_BROWSER !== "false",
      },
    },
  },
];

const modules = {
  /*eventBus: {
    resolve: "@medusajs/event-bus-redis",
    options: {
      redisUrl: REDIS_URL
    }
  },
  cacheService: {
    resolve: "@medusajs/cache-redis",
    options: {
      redisUrl: REDIS_URL
    }
  },*/
};

/** @type {import('@medusajs/medusa').ConfigModule["projectConfig"]} */
const projectConfig = {
  jwtSecret: process.env.JWT_SECRET,
  cookieSecret: process.env.COOKIE_SECRET,
  store_cors: STORE_CORS,
  database_url: DATABASE_URL,
  admin_cors: ADMIN_CORS,
  // Uncomment the following lines to enable REDIS
  // redis_url: REDIS_URL
};

/** @type {import('@medusajs/medusa').ConfigModule} */
module.exports = {
  projectConfig,
  plugins,
  modules,
};

My copy of node_modules/@medusajs/admin-ui/ui/src/constants/medusa-backend-url.ts reads only:

export const MEDUSA_BACKEND_URL = "http://[[myDomainName]]:9000" 

The default copy of node_modules/@medusajs/admin-ui/ui/src/constants/medusa-backend-url.ts used to be:

export const MEDUSA_BACKEND_URL = process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"

I also found what I believe to be the source of the problem that caused process.env.MEDUSA_BACKEND_URL to be overwritten. It seems like in node_modules/@medusajs/medusa/dist/commands/develop.js, there is a hardcoded reference to localhost whenever the medusa CLI calls the medusa-admin CLI. The medusa-admin CLI has a "develop" command with a "--backend" option to specify where the admin pages should look to call the backend, but the medusa CLI only ever calls "medusa-admin develop" with "--backend" equal to "localhost:9000"

Changing line 139 in node_modules/@medusajs/medusa/dist/commands/develop.js from:

backendUrl = "http://localhost:".concat(port);

and replacing it with

backendUrl = process.env.MEDUSA_BACKEND_URL.concat(":").concat(port);

or whatever process.env variable name you give your backend URL should be another semi-permanent solution, at least until your node_modules get updated.

Make sure there are no trailing "/"s on your backend url value in your process.env or the admin will look for the backend at http://domainname.com/:9000 !

norrisduncan avatar Nov 21 '23 03:11 norrisduncan

Finally it worked! Thank you, Sir. As is the case with you, I'm not sure exactly which of all the changes I made did the trick so I'll experiment over the next few days to pinpoint it. One other thing I noticed is that when I use the medusa start command (instead of medusa develop), the admin dashboard is not started even though the serve flag is set to true in my medusa-config.json file.

On a related note, should we open a PR to get this fixed? Medusa ignoring environment variables is a bug no?

faridghar avatar Nov 21 '23 06:11 faridghar

Looks like I might have worked around the problem by using SSH's port forwarding facility (-L switch).

example:

ssh -L localhost:7001:localhost:7001 -L localhost:9000:localhost:9000 username@Remote_OR_local_IP

New to MedusaJS so the below console errors are hopefully normal? (image to follow):

2023-11-29_09-56

slapscrap avatar Nov 29 '23 10:11 slapscrap

Yeah, I believe those batch jobs errors have to do with the fact that you probably don't have redis up and running. If I'm not mistaken, the batch job functionality of Medusa requires redis. Try installing and running a redis instance locally and then comment out the lines in medusa-config to enable Redis...they should disappear.

faridghar avatar Nov 29 '23 10:11 faridghar

Not sure if anyone is following this issue anymore, but if I use a hack in develop.ts command then I get the right url, but since it's not on the same domain I run into cors issue.

dmiric avatar Apr 15 '24 13:04 dmiric

Hey, thanks for the report! Since v2 brought a lot of architectural and API changes on the backend, we will be closing this ticket since it no longer applies to our new setup, or the issue has already been fixed. If you are still facing issues with v1, please open a new ticket and we will address it as soon as possible. Thanks! 🙏

sradevski avatar Jul 05 '24 10:07 sradevski