storacha-network / w3up

⁂ w3up protocol implementation
https://github.com/storacha-network/specs
Other
54 stars 19 forks source link

[ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './ed25519' is not defined by "exports" #1255

Closed HernanPaez closed 7 months ago

HernanPaez commented 7 months ago

Hi! I am trying to replace the old web3storage library with the new one but I am having problems when I try to run my server

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './ed25519' is not defined by "exports" in /Users//server-proyect/node_modules/@ucanto/principal/package.json

I don't know how to fix this issue. This is a new typescript project

Here is my code:

import { Signer } from '@ucanto/principal/ed25519'
import { importDAG } from "@ucanto/core/delegation";
import { CarReader } from "@ipld/car";
import { create, Client } from "@web3-storage/w3up-client";

class Web3StorageClient {
  client?: Client;

  async init() {
    if (process.env.W3_KEY && process.env.W3_PROOF) {
      const principal = Signer.parse(process.env.W3_KEY);
      const client = await create({ principal });

      // now give Agent the delegation from the Space
      const proof = await this.parseProof(process.env.W3_PROOF);
      const space = await client.addSpace(proof);
      await client.setCurrentSpace(space.did());
    } else {
      throw "check W3_KEY and W3_PROOF environment variables";
    }
  }

  /** @param {string} data Base64 encoded CAR file */
  async parseProof(data: string) {
    const blocks: any = [];
    const reader = await CarReader.fromBytes(Buffer.from(data, "base64"));
    for await (const block of reader.blocks()) {
      blocks.push(block);
    }
    return importDAG(blocks);
  }

  async storeFile(id: string, bufferPdf: any, extension = "pdf") {
    if (!this.client) {
      throw "Client has not been initialized";
    }

    const file = new File([bufferPdf], `${id}.${extension}`);
    const cid = await this.client.uploadFile(file);
    return cid.toString();
  }

  async createNftMetadata(nftRequestJson: File[]) {
    const res = await this.client?.uploadDirectory(nftRequestJson);
    return res?.toString();
  }
}

export default Web3StorageClient;
magror commented 7 months ago

This is also happening to me. In fact, attempting to import any of the 4 necessary dependencies causes errors; '@ucanto/principal/ed25519', '@ucanto/core/delegation', '@ipld/car', '@web3-storage/w3up-client' all cannot be imported/accessed.

I am attempting to import and access these dependencies in a Docker image with Typescript.

When I try to isolate "the" issue to a single dependency by only importing/accessing one dependency at a time, I get the following errors;

'@ucanto/principal/ed25519': { "errorType": "Error", "errorMessage": "Package subpath './ed25519' is not defined by \"exports\" in /var/task/node_modules/@ucanto/principal/package.json", "code": "ERR_PACKAGE_PATH_NOT_EXPORTED", "stack": [ "Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './ed25519' is not defined by \"exports\" in /var/task/node_modules/@ucanto/principal/package.json", " at new NodeError (node:internal/errors:405:5)", " at exportsNotFound (node:internal/modules/esm/resolve:367:10)", " at packageExportsResolve (node:internal/modules/esm/resolve:647:13)", " at resolveExports (node:internal/modules/cjs/loader:567:36)", " at Module._findPath (node:internal/modules/cjs/loader:636:31)", " at Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)", " at Module._load (node:internal/modules/cjs/loader:922:27)", " at Module.require (node:internal/modules/cjs/loader:1143:19)", " at require (node:internal/modules/cjs/helpers:119:18)", " at Object.<anonymous> (/var/task/util/ipfs.js:32:29)" ] }

'@ucanto/core/delegation': { "errorType": "Error", "errorMessage": "Package subpath './delegation' is not defined by \"exports\" in /var/task/node_modules/@ucanto/core/package.json", "code": "ERR_PACKAGE_PATH_NOT_EXPORTED", "stack": [ "Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './delegation' is not defined by \"exports\" in /var/task/node_modules/@ucanto/core/package.json", " at new NodeError (node:internal/errors:405:5)", " at exportsNotFound (node:internal/modules/esm/resolve:367:10)", " at packageExportsResolve (node:internal/modules/esm/resolve:647:13)", " at resolveExports (node:internal/modules/cjs/loader:567:36)", " at Module._findPath (node:internal/modules/cjs/loader:636:31)", " at Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)", " at Module._load (node:internal/modules/cjs/loader:922:27)", " at Module.require (node:internal/modules/cjs/loader:1143:19)", " at require (node:internal/modules/cjs/helpers:119:18)", " at Object.<anonymous> (/var/task/util/ipfs.js:32:22)" ] }

'@ipld/car': { "errorType": "Error", "errorMessage": "No \"exports\" main defined in /var/task/node_modules/@ipld/car/package.json", "code": "ERR_PACKAGE_PATH_NOT_EXPORTED", "stack": [ "Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No \"exports\" main defined in /var/task/node_modules/@ipld/car/package.json", " at new NodeError (node:internal/errors:405:5)", " at exportsNotFound (node:internal/modules/esm/resolve:367:10)", " at packageExportsResolve (node:internal/modules/esm/resolve:647:13)", " at resolveExports (node:internal/modules/cjs/loader:567:36)", " at Module._findPath (node:internal/modules/cjs/loader:636:31)", " at Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)", " at Module._load (node:internal/modules/cjs/loader:922:27)", " at Module.require (node:internal/modules/cjs/loader:1143:19)", " at require (node:internal/modules/cjs/helpers:119:18)", " at Object.<anonymous> (/var/task/util/ipfs.js:32:15)" ] }

'@web3-storage/w3up-client': { "errorType": "Error", "errorMessage": "require() of ES Module /var/task/node_modules/@web3-storage/w3up-client/dist/src/index.node.js from /var/task/util/ipfs.js not supported.\nInstead change the require of index.node.js in /var/task/util/ipfs.js to a dynamic import() which is available in all CommonJS modules.", "code": "ERR_REQUIRE_ESM", "stack": [ "Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/node_modules/@web3-storage/w3up-client/dist/src/index.node.js from /var/task/util/ipfs.js not supported.", "Instead change the require of index.node.js in /var/task/util/ipfs.js to a dynamic import() which is available in all CommonJS modules.", " at Object.<anonymous> (/var/task/util/ipfs.js:32:29)", " at Object.<anonymous> (/var/task/handlers/metadata.js:4:16)", " at Object.<anonymous> (/var/task/index.js:10:20)", " at _tryRequireFile (file:///var/runtime/index.mjs:1002:37)", " at _tryRequire (file:///var/runtime/index.mjs:1052:25)", " at _loadUserApp (file:///var/runtime/index.mjs:1081:22)", " at UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:27)", " at start (file:///var/runtime/index.mjs:1282:42)", " at file:///var/runtime/index.mjs:1288:7" ] }

thanapat-civicledger commented 7 months ago

I'm experiencing the same problem. To provide additional context, my code is executed within a Docker environment, and I'm using Node.js version 16.20.2.

heyjay44 commented 7 months ago

@HernanPaez which version of node are you using? The client requires version 18 or higher.

@thanapat-civicledger let us know if the issue persists after upgrading node.

heyjay44 commented 7 months ago

@magror which version of node are you using?

magror commented 7 months ago

@magror which version of node are you using?

@heyjay44 Im using Node 18

My Dockerfile uses Node 18, too: FROM public.ecr.aws/lambda/nodejs:18

vasco-santos commented 7 months ago

Hey folks @HernanPaez @magror

Looks like this is a result of typescript project being compiled to CJS, which is not supported anymore by web3.storage. Like most of JS ecosystem we are moving towards ESM only module publishing.

I created a local typescript project where I made package.json to have a "type": "module", + on your tsconfig.json I updated the module to NodeNext. Can you try it out?

magror commented 7 months ago

@vasco-santos What is "CSM"? I cannot find any information about that acronym. Did you mean "CJS"?

vasco-santos commented 7 months ago

Bahm, yes CJS 🤦🏼‍♂️

magror commented 7 months ago

@vasco-santos adding type: module to package.json and setting module: NodeNext in tsconfig.json has allowed me to import/access this dependency. Thank you