js-ipfs-utils icon indicating copy to clipboard operation
js-ipfs-utils copied to clipboard

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

Open Rinse12 opened this issue 3 years ago • 3 comments

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 avatar Jan 23 '22 10:01 Rinse12