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

fix .cjs and package.json in distribution #52

Merged
merged 4 commits into from
Sep 11, 2024

Conversation

heyAyushh
Copy link
Contributor

@heyAyushh heyAyushh commented Sep 10, 2024

Problems

  • CJS version of distribution is giving error for Node.JS
Error [ERR_REQUIRE_ESM]: require() of ES Module /project/workspace/node_modules/@solana-developers/helpers/dist/cjs/index.js from /project/workspace/index.js not supported.
/project/workspace/node_modules/@solana-developers/helpers/dist/cjs/index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename /project/workspace/node_modules/@solana-developers/helpers/dist/cjs/index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /project/workspace/node_modules/@solana-developers/helpers/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/project/workspace/index.js:1:26) {
  code: 'ERR_REQUIRE_ESM'
}
  • ESM version of distribution is not getting recognised for Node.JS when executed with .mjs extension
  • ESM Exports are not correct
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/project/workspace/node_modules/@solana-developers/helpers/dist/esm/lib/keypair' imported from /project/workspace/node_modules/@solana-developers/helpers/dist/esm/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///project/workspace/node_modules/@solana-developers/helpers/dist/esm/lib/keypair'
}

Solutions

  • Add a post-build.mjs script to process files further that tsc isn't doing
  • Add package.json in cjs and esm distributions at build step, (escaping fallback of package.json to default one)
  • rename exports and imports with .js for esm builds
  • Add types as a distribution folder
  • Refactor tsconfig.json(s) to have non redundant ts declarations (exclude declarations from esm and cjs dists)

post-build.mjs Show resolved Hide resolved
post-build.mjs Show resolved Hide resolved
@mikemaccana mikemaccana merged commit 9bbba20 into solana-developers:main Sep 11, 2024
1 check 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.

2 participants