ipfs / js-ipfs-utils

IPFS utils
Other
23 stars 30 forks source link

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

Open Rinse12 opened 2 years ago

Rinse12 commented 2 years ago

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 commented 2 years ago

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

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

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