Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make internal imports more friendly for users using ES Modules #53

Merged
merged 1 commit into from
Feb 8, 2025

Conversation

simonmcallister0210
Copy link
Owner

Context

A user noticed an error when trying to import the package when using ES Modules:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/project/node_modules/.pnpm/[email protected]/node_modules/buffer/' is not supported resolving ES modules imported from /project/node_modules/.pnpm/[email protected]/node_modules/cognito-srp-helper/dist/esm/index.js
Did you mean to import [email protected]/node_modules/buffer/index.js?
    at finalizeResolution (node:internal/modules/esm/resolve:249:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///project/node_modules/.pnpm/[email protected]/node_modules/buffer/'

We're using the buffer package so that our package can work in both NodeJS and browsers. To "tell" the code to import this version of Buffer, and not the built-in version, you have to import it via a directory and not the package name. Importing from buffer/ is problematic, but buffer/index.js is fine

Whilst fixing this issue I noticed it had a problem with me import { lib, ... } from crypto-js. Using the default import fixed that

And finally I also noticed the package doesn't have a default export, so I added that too

Changes

  • Make buffer imports more explicit
  • Use default import for crypto-js
  • Provide a default export

Testing

@wingy3181 kindly provided a repo to replicate the issue, along with some test cases. Here's the test cases succeeding with the modified version of the package:

Screenshot 2025-02-01 at 22 10 26

@simonmcallister0210 simonmcallister0210 linked an issue Feb 1, 2025 that may be closed by this pull request
@simonmcallister0210 simonmcallister0210 merged commit 2016d42 into main Feb 8, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ES Module error
1 participant