medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.92k stars 2.6k forks source link

Docs(Code Issue): Medusa object not constructable (JS SDK) #9698

Open Ketlark opened 3 weeks ago

Ketlark commented 3 weeks ago

What Medusa version and documentation are you using?

v2

Preliminary Checks

Issue Summary

I want to use medusa JS SDK to perform call on backend server, but when i use the default sample code from documentation, the medusa object is not constructable:

image

How can this issue be resolved?

Issue can be solved by calling new Medusa.default

Are you interested in working on this issue?

shahednasser commented 3 weeks ago

Hello, can you clarify which code snippet you used from the docs? And which page are you referring to?

Ketlark commented 3 weeks ago

Of course, it's this part : https://docs.medusajs.com/v2/resources/js-sdk#setup-js-sdk. I get rid of error but can't still used the SDK normally, even after tried multiple class import from it

shahednasser commented 3 weeks ago

Can you share your code block? Also, what type of project are you using it in?

Ketlark commented 3 weeks ago
import env from '#start/env'
import Medusa from '@medusajs/js-sdk'

export default class MedusaService {
  private static instance: Medusa.Admin
  private static MEDUSA_API_KEY = env.get('MEDUSA_API_KEY')

  static getInstance() {
    if (!MedusaService.instance) {
      MedusaService.instance = new Medusa.default({
        baseUrl: 'https://erp.mullo.fr',
        apiKey: this.MEDUSA_API_KEY,
      }).admin
    }
    return MedusaService.instance
  }
}

A sample nodeJS backend project that use Medusa backend API

thetutlage commented 3 weeks ago

Can you please share the tsconfig of your project?

Ketlark commented 3 weeks ago

@thetutlage here is it

{
  "extends": "@adonisjs/tsconfig/tsconfig.app.json",
  "compilerOptions": {
    "jsx": "react",
    "rootDir": "./",
    "outDir": "./build",
  },
}

with base :


{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "lib": ["ESNext"],
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "isolatedModules": true,
    "removeComments": true,
    "esModuleInterop": true,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "strictPropertyInitialization": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "skipLibCheck": true
  },
  "ts-node": {
    "swc": true
  }
}
thetutlage commented 3 weeks ago

Hello @Ketlark

I looked into the issue and it seems like our JS SDK is not compatible with the ESM implementation of Node.js and therefore it is not working at runtime and have type errors as well.

I will be working on it in the coming days and will let you know.

Ketlark commented 2 weeks ago

@thetutlage any news or blocking steps ? maybe i can help to fix it

thetutlage commented 1 week ago

Hello @Ketlark

Sorry for the late reply. We will discussing on fixing this issue this Monday internally and will keep you posted about it.