Skip to content

fix(js-sdk): add explicit .js extensions to relative imports for ESM compliance#15037

Open
pevey wants to merge 2 commits intomedusajs:developfrom
pevey:fix/js-sdk-esm-imports
Open

fix(js-sdk): add explicit .js extensions to relative imports for ESM compliance#15037
pevey wants to merge 2 commits intomedusajs:developfrom
pevey:fix/js-sdk-esm-imports

Conversation

@pevey
Copy link
Copy Markdown
Contributor

@pevey pevey commented Apr 8, 2026

Fixes #9573

Summary

  • Adds explicit .js extensions to all relative imports in @medusajs/js-sdk source files
  • Fixes Node.js native ESM resolution failures during SSR in SvelteKit, Astro, and other Vite-based frameworks
  • Backwards-compatible with CJS and bundler consumers (Next.js, webpack, etc.)

Root Cause

The ESM build output (dist/esm/) contained bare directory imports like export from './admin' and extensionless file imports like import from './client'. These work in bundlers but fail under Node.js native ESM, which strictly requires explicit paths (./admin/index.js, ./client.js).

Frameworks like SvelteKit and Astro use Node's native ESM loader for SSR, causing runtime import resolution errors.

Test plan

  • yarn workspace @medusajs/js-sdk test — all 20 tests pass
  • yarn workspace @medusajs/js-sdk build — builds successfully
  • Verified dist/esm/index.js output contains proper .js extensions
  • Verified CJS require() still works
  • Verified ESM import works under Node.js native ESM
  • Tested in SvelteKit storefront (SSR) — resolved the import error
  • Tested in Astro storefront — resolved the import error

Note

Low Risk
Low risk: this is a mechanical import-path change to improve Node native ESM resolution, with no runtime logic changes. Main risk is accidental missed/incorrect paths causing module-resolution failures in some build targets.

Overview
Fixes Node native ESM/SSR consumption of @medusajs/js-sdk by making all internal relative imports explicitly include .js (and using ./*/index.js where applicable).

Updates the public entry (src/index.ts), client.ts, and the admin/auth/store modules to use ESM-compliant import specifiers, and adds a patch changeset for the release.

Reviewed by Cursor Bugbot for commit a9fa38c. Bugbot is set up for automated code reviews on this repo. Configure here.

…compliance

The compiled ESM output contained bare directory imports (e.g., `export from './admin'`)
which fail under Node.js native ESM resolution used by SvelteKit and Astro during SSR.
Adding explicit `.js` extensions ensures the ESM output is spec-compliant while remaining
backwards-compatible with CJS and bundler consumers.

Fixes medusajs#9573

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@pevey pevey requested a review from a team as a code owner April 8, 2026 23:13
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 8, 2026

🦋 Changeset detected

Latest commit: a9fa38c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 77 packages
Name Type
@medusajs/js-sdk Patch
@medusajs/draft-order Patch
@medusajs/dashboard Patch
@medusajs/medusa Patch
@medusajs/admin-bundler Patch
@medusajs/test-utils Patch
@medusajs/medusa-oas-cli Patch
integration-tests-http Patch
@medusajs/analytics Patch
@medusajs/api-key Patch
@medusajs/auth Patch
@medusajs/caching Patch
@medusajs/cart Patch
@medusajs/currency Patch
@medusajs/customer Patch
@medusajs/file Patch
@medusajs/fulfillment Patch
@medusajs/index Patch
@medusajs/inventory Patch
@medusajs/link-modules Patch
@medusajs/locking Patch
@medusajs/notification Patch
@medusajs/order Patch
@medusajs/payment Patch
@medusajs/pricing Patch
@medusajs/product Patch
@medusajs/promotion Patch
@medusajs/rbac Patch
@medusajs/region Patch
@medusajs/sales-channel Patch
@medusajs/settings Patch
@medusajs/stock-location Patch
@medusajs/store Patch
@medusajs/tax Patch
@medusajs/translation Patch
@medusajs/user Patch
@medusajs/workflow-engine-inmemory Patch
@medusajs/workflow-engine-redis Patch
@medusajs/oas-github-ci Patch
@medusajs/cache-inmemory Patch
@medusajs/cache-redis Patch
@medusajs/event-bus-local Patch
@medusajs/event-bus-redis Patch
@medusajs/analytics-local Patch
@medusajs/analytics-posthog Patch
@medusajs/auth-emailpass Patch
@medusajs/auth-github Patch
@medusajs/auth-google Patch
@medusajs/caching-redis Patch
@medusajs/file-local Patch
@medusajs/file-s3 Patch
@medusajs/fulfillment-manual Patch
@medusajs/locking-postgres Patch
@medusajs/locking-redis Patch
@medusajs/notification-local Patch
@medusajs/notification-sendgrid Patch
@medusajs/payment-stripe Patch
@medusajs/core-flows Patch
@medusajs/framework Patch
@medusajs/modules-sdk Patch
@medusajs/orchestration Patch
@medusajs/types Patch
@medusajs/utils Patch
@medusajs/workflows-sdk Patch
@medusajs/http-types-generator Patch
@medusajs/cli Patch
@medusajs/deps Patch
@medusajs/telemetry Patch
@medusajs/admin-sdk Patch
@medusajs/admin-shared Patch
@medusajs/admin-vite-plugin Patch
@medusajs/icons Patch
@medusajs/toolbox Patch
@medusajs/ui-preset Patch
create-medusa-app Patch
medusa-dev-cli Patch
@medusajs/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 2026

@pevey is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

@medusa-os-bot
Copy link
Copy Markdown

medusa-os-bot bot commented Apr 9, 2026

Thank you for your contribution!

After reviewing this PR, we need a few things addressed before we can move forward:

Required changes:

  • Add a changeset for @medusajs/js-sdk — run yarn changeset, select @medusajs/js-sdk as a patch bump, and use the message format fix(js-sdk): add explicit .js extensions to relative imports for ESM compliance
  • This PR touches 53 files, which exceeds our threshold for large community contributions. Large PRs require the linked issue to be marked help-wanted by the team before work begins. Could you comment on issue [@medusajs/js-sdk] ERR_UNSUPPORTED_DIR_IMPORT when using the js-sdk #9573 asking the team to label it help-wanted? This helps us ensure contributions are pre-approved and reduces the risk of wasted effort. See CONTRIBUTING.md for more details.

Once these are addressed, another review will be triggered automatically.

Thanks!

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@pevey
Copy link
Copy Markdown
Contributor Author

pevey commented Apr 9, 2026

A note on the direction of the PR: Strict ESM compliance in the compiled output could also be accomplished by only touching the tsconfig, which would seem to be a smaller change. But after trial and error with lots of different settings (with the help of Claude), I couldn't find a way of taking that approach that ALSO preserved cjs compatibility. That was the reason for this approach. I wanted to make sure the change did not cause any loss of current compatibility. If cjs compatibility is no longer a concern, a tsconfig change would be cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@medusajs/js-sdk] ERR_UNSUPPORTED_DIR_IMPORT when using the js-sdk

1 participant