MetaMask / key-tree

MIT License
49 stars 19 forks source link

8.0.0 fails to use esm in node #144

Closed hugomrdias closed 1 year ago

hugomrdias commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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