multiformats / js-multiformats

Multiformats interface (multihash, multicodec, multibase and CID)
Other
235 stars 54 forks source link

Error with installation: Package subpath './cid' is not defined by "exports" #237

Closed Latrasis closed 1 year ago

Latrasis commented 1 year ago

On installation ^11.0.0 or ^10.0.0 and running ts-node:v7.0.1

import { CID } from "multiformats/cid";
import * as json from "multiformats/codecs/json";
import { sha256 } from "multiformats/hashes/sha2";
import { base64 } from "multiformats/bases/base64"

function foo(){
   const bytes = json.encode({"foo":"bar"});
}

I get:

Error: Package subpath './codecs/json' is not defined by "exports"  in .../node_modules/multiformats/package.json
    at new NodeError (node:internal/errors:400:5)
    at exportsNotFound (node:internal/modules/esm/resolve:361:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:641:13)
    at resolveExports (node:internal/modules/cjs/loader:538:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:607:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
    at Function.Module._resolveFilename (../node_modules/ts-mocha/node_modules/tsconfig-paths/src/register.ts:90:36)
    at Function.Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Using node version: Node.js v18.13.0 Npm version: 8.19.3

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2021" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "strict": true /* Enable all strict type-checking options. */,
    "types": ["node", "mocha"] /* Type declaration files to be included in compilation. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,

    "skipLibCheck": false /* Skip type checking of declaration files. */,
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  }
}
rvagg commented 1 year ago

I suspect this is because of "module": "commonjs"; we're only exporting ESM now and I guess if you're asking typescript to compile to require('multiformats/cid') etc. then that's not going to fly. You'll either need to use <10 or switch to one of the ESM compile targets.

RangerMauve commented 1 year ago

Reproduced this in Electron 23 with ESM imports. It was due to using require.

Electron doesn't allow using import for node modules which is a bit wonky. I'll post here if I find a workaround.

BigLep commented 1 year ago

2023-02-21 maintainer conversation: unfortunatley we can't support this. We're only able to do ESM publishing, but we recognize that impacts certain toolchains haven't updated yet. v10 of multiformats should fine for you for now until Electron upgrades.