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

Unable to import ipfs-http-client due to ipfs-utils using require statement #171

Open
Rinse12 opened this issue Jan 23, 2022 · 3 comments
Open
Labels
need/triage Needs initial labeling and prioritization

Comments

@Rinse12
Copy link

Rinse12 commented Jan 23, 2022

Hello team,

I'm receiving an error whenever I try to import ipfs-http-client. I realize this is js-ipfs-utils repo but the error is orignating from this repo's code.

My code

import { create } from 'ipfs-http-client'

Error when I run mocha test

Error [ERR_REQUIRE_ESM]: require() of ES Module plebbit/plebbit.js/node_modules/node-fetch/src/index.js from plebbit/plebbit.js/node_modules/native-fetch/src/index.js not supported.
Instead change the require of plebbit/plebbit.js/node_modules/node-fetch/src/index.js in plebbit/plebbit.js/node_modules/native-fetch/src/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (plebbit/plebbit.js/node_modules/native-fetch/src/index.js:12:14)
    at Object.<anonymous> (plebbit/plebbit.js/node_modules/ipfs-utils/src/fetch.js:9:20)
    at Object.<anonymous> (plebbit/plebbit.js/node_modules/ipfs-utils/src/http/fetch.node.js:2:62)
    at Object.<anonymous> (plebbit/plebbit.js/node_modules/ipfs-utils/src/http/fetch.js:8:20)
    at Object.<anonymous> (plebbit/plebbit.js/node_modules/ipfs-utils/src/http.js:4:37)
    at async Promise.all (index 0)
    at async formattedImport (plebbit/plebbit.js/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (plebbit/plebbit.js/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (plebbit/plebbit.js/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at async singleRun (plebbit/plebbit.js/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (plebbit/plebbit.js/node_modules/mocha/lib/cli/run.js:374:5)

The line in ipfs-utils/src/fetch.js(9) using require and causing the problem

  module.exports = require('native-fetch')

My package.json

{
  "name": "plebbit.js",
  "version": "0.0.1",
  "description": "plebbit.js will be an NPM module to wrap around the IPFS APIs used by Plebbit. It will be used in all clients: CLI, Electron (Desktop GUI) and Web.",
  "main": "src/Plebbit.js",
  "scripts": {
    "test": "mocha --timeout 5000"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/plebbit/plebbit-js.git"
  },
  "keywords": [
    "plebbit"
  ],
  "author": "Esteban Abaroa, Rinse",
  "license": "GPL-2.0-only",
  "bugs": {
    "url": "https://github.com/plebbit/plebbit-js/issues"
  },
  "homepage": "https://github.com/plebbit/plebbit-js#readme",
  "dependencies": {
    "ipfs-http-client": "^55.0.0"
  },
  "devDependencies": {
    "mocha": "^9.1.4"
  },
  "type": "module"
}
@Rinse12 Rinse12 added the need/triage Needs initial labeling and prioritization label Jan 23, 2022
@Rinse12
Copy link
Author

Rinse12 commented Jan 23, 2022

I fixed the issue by explicitly install node-fetch@2 instead of version 3 where it's required to use ES module.

"dependencies": {
    "node-fetch": "^2.6.7",
    "ipfs-core": "^0.13.0",
    "ipfs-http-client": "^55.0.0"
  },

@achingbrain
Copy link
Member

node-fetch is a peer dependency of native-fetch - this module has a dependency on node-fetch@2.x.x which is CJS so you should not need to install it yourself.

If you are seeing ERR_REQUIRE_ESM it means a copy of node-fetch@3 is in your dependency tree somewhere.

When you don't install the dep yourself, what do you see when you run:

$ npm ls node-fetch

@talentlessguy
Copy link

talentlessguy commented Mar 20, 2023

would a PR with ESM rewrite be welcome? ipfs-http-client is already ESM so I don't think this should be a problem to use ipfs-utils with modern Node.js

@talentlessguy talentlessguy mentioned this issue Mar 20, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/triage Needs initial labeling and prioritization
Projects
None yet
Development

No branches or pull requests

3 participants