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

8.0.0 fails to use esm in node #144

Closed
hugomrdias opened this issue Jul 12, 2023 · 3 comments · Fixed by #147
Closed

8.0.0 fails to use esm in node #144

hugomrdias opened this issue Jul 12, 2023 · 3 comments · Fixed by #147

Comments

@hugomrdias
Copy link

the dual packaging setup does not support esm version in node and cjs version does not do named exports correctly

import { getBIP44AddressKeyDeriver } from '@metamask/key-tree';
         ^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'getBIP44AddressKeyDeriver' not found. The requested module '@metamask/key-tree' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@metamask/key-tree';
const { getBIP44AddressKeyDeriver } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

More information about dual packaging here https://github.com/sheremet-va/dual-packaging.

@Mrtenz
Copy link
Member

Mrtenz commented Jul 12, 2023

I hadn't considered that people were running Node.js in ESM mode. We made the change primarily to support Webpack and other bundlers, which use the module field, but I guess Node.js doesn't, even when ESM mode is enabled?

@Mrtenz
Copy link
Member

Mrtenz commented Jul 12, 2023

@hugomrdias Can you try adding this to the package.json of key-tree in your node_modules (just for testing), and let me know if that resolves it?

  "exports": {
    ".": {
      "import": "./dist/esm/index.js",
      "require": "./dist/cjs/index.js",
      "types": "./dist/types/index.d.ts"
    },
    "./dist/cjs/*": {
      "require": "./dist/cjs/*.js",
      "types": "./dist/types/*.d.ts"
    },
    "./dist/esm/*": {
      "import": "./dist/esm/*.js",
      "types": "./dist/types/*.d.ts"
    }
  },

@hugomrdias
Copy link
Author

you need to add a package.json file in the esm folder with type: module, its all the link

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 a pull request may close this issue.

2 participants