ipfs / js-ipfs

IPFS implementation in JavaScript
https://js.ipfs.tech
Other
7.44k stars 1.25k forks source link

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in .../node_modules/ipfs-http-client/package.json #4180

Closed rawatnaresh closed 2 years ago

rawatnaresh commented 2 years ago

Severity:

High

Description:

I'm trying to use ipfs-http-client with Firebase cloud functions but I'm getting an error while importing it.

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in .../node_modules/ipfs-http-client/package.json

Steps to reproduce the error:


- Run the app 
- Here's the reproducible demo https://github.com/rawatnaresh/firebase-functions-ipfs
welcome[bot] commented 2 years ago

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review. In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additional round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment. Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

Finally, remember to use https://discuss.ipfs.io if you just need general support.

ogubuikeAlex commented 2 years ago

I also have this issue.

I have a basic implementation:

import { create } from "ipfs-http-client";

const client = create();
async function addFiles(arrayOfFiles) {

  const result = [];

  for await (const resultPart of client.addAll(arrayOfFiles)) {
    result.push(resultPart);
  }
  }
rawatnaresh commented 2 years ago

@king-Alex-d-great you would have to change your firebase-functions to use ES modules for this package to work. By default firebase functions use commonjs. In order to do that add "type": "module" in your package.json file.

markg85 commented 2 years ago
const { create } = require('ipfs-http-client')
const client = create();

where the project type is nothing or commonjs (nothing defaults to commonjs) gives the same error.

Currently that means i must convert to a "type":"module" or downgrade to the previous release where this was working just fine.

kenshyx commented 2 years ago

I'm curious why the main attribute from package.json was removed, this would serve as a fallback for cjs imports. Not everyone can easily migrate their project to use "type":"module"

threshold862543 commented 2 years ago

Downgrading to ipfs-http-client 56.0.0 fixed this for me.

CorpulentBrony commented 2 years ago

Having this same issue.

CorpulentBrony commented 2 years ago

Not sure why nobody's come along to let this thread know, but apparently you can use dynamic imports in CJS apps still: https://github.com/ipfs/js-ipfs/blob/master/docs/upgrading/v0.62-v0.63.md

silasdavis commented 2 years ago

Not sure why nobody's come along to let this thread know, but apparently you can use dynamic imports in CJS apps still: https://github.com/ipfs/js-ipfs/blob/master/docs/upgrading/v0.62-v0.63.md

This did not work for me, not sure if it requires additional tsconfig properties, but I get the same ERR_PACKAGE_PATH_NOT_EXPORTED thrown from the dynamic import. I'm using esModuleInterop: true and running via ts-node, which may be relevant.

CorpulentBrony commented 2 years ago

This did not work for me, not sure if it requires additional tsconfig properties, but I get the same ERR_PACKAGE_PATH_NOT_EXPORTED thrown from the dynamic import. I'm using esModuleInterop: true and running via ts-node, which may be relevant.

Not working for me either.

achingbrain commented 2 years ago

Your tsconfig is set to output CJS - it needs to output ESM instead.

Add "type": "module" to your package.json, and in tsconfig.json change "module" to "ES2020" and add "moduleResolution": "node".

CorpulentBrony commented 2 years ago

Your tsconfig is set to output CJS - it needs to output ESM instead.

Add "type": "module" to your package.json, and in tsconfig.json change "module" to "ES2020" and add "moduleResolution": "node".

That doesn't fix the issue when your entire codebase is CJS you can't just switch to module without refactoring everything, including other third party packages that simply won't include properly as a module.

Also, in my case I'm not using Typescript

achingbrain commented 2 years ago

@CorpulentBrony sorry it wasn't clear - I was replying to the OP, who has supplied a repro case that needs an update to it's tsconfig.json for it to work.

If you have a repro case I can take a look, but as you've found if you are using CJS and cannot upgrade, you need to use a dynamic import to load ESM modules.

daenamkim commented 1 year ago

@achingbrain As @CorpulentBrony said it does not make sense to me as well because I have tested ESM config breaks everything.

If you have a repro case I can take a look, but https://github.com/ipfs/js-ipfs/issues/4180#issuecomment-1250182601 if you are using CJS and cannot upgrade, you need to use a dynamic import to load ESM modules.

I could not understand why and all context of this. I just wonder if there is no possibility to go back to CJS, the ultimate solution is just to keep 56.x at the moment? (FYI, I am using typescript)

hubsmoke commented 1 year ago

I figured out a magical incantation of tsconfig.json and package.json that allows us to use ipfs-http-api via typescript import

Link to minimum good configuration example repo: https://github.com/hubsmoke/ipfs-typescript-starter (run npm run start:dev to execute)

achingbrain commented 1 year ago

@hubsmoke this almost works but the npm run build command in your repo fails. To get it to work set target and module to "es2020" and remove "lib":

{
  "compilerOptions": {
    "target": "es2020",
    "module": "es2020",
    "moduleResolution": "node",
    "allowJs": true,
    "outDir": "build",
    "rootDir": "src",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
  },
  "ts-node": {
    "esm": true
  },
}
achingbrain commented 1 year ago

There's more info in the upgrade guide

hubsmoke commented 1 year ago

Awesome thanks for catching that. Here's the diff following your suggestions, which did improve the situation: https://github.com/hubsmoke/ipfs-typescript-starter/commit/58837da953946bd3b513fb90476b3cb4a5de4c33

However it still results in:

> ipfs-experiment@1.0.0 build
> rimraf ./build && tsc

node_modules/ipfs-utils/dist/src/files/glob-source.d.ts:7:20 - error TS2307: Cannot find module 'ipfs-unixfs/types/src/types' or its corresponding type declarations.

7     mtime?: import("ipfs-unixfs/types/src/types").MtimeLike | undefined;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/ipfs-utils/dist/src/files/glob-source.d.ts:12:19 - error TS2307: Cannot find module 'ipfs-unixfs/types/src/types' or its corresponding type declarations.

12     mtime: import("ipfs-unixfs/types/src/types").MtimeLike | undefined;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 2 errors in the same file, starting at: node_modules/ipfs-utils/dist/src/files/glob-source.d.ts:7

I will need to tweak this to fix this issue -- I've run into it in the past just forgot what it needs. Took a look at the upgrade guide, but unclear what I would need to do to fix the above, lmk if you have any ideas. Thanks for your help! IMO would be great to have a community working template to get folks going on IPFS quickly.

achingbrain commented 1 year ago

Delete your lock file and reinstall

hubsmoke commented 1 year ago

@achingbrain awesome! Had to nuke my node_modules as well. Thanks for your help & attention 💯