0

I'm trying to create a project which deploys nfts to ipfs automatically. I downloaded an existing project and I'm trying to extend the functionality

My packages.json looks like so:

{
  "name": "3_market_engine",
  "main": "index.js",
  "bin": "index.js",
  "pkg": {
    "assets": [
      "layers/**/*",
      "node_modules/**/*",
      "src/**/*"
    ]
  },
  "scripts": {
    "build": "node index.js",
    "generate": "node index.js",
    "generate_metadata": "node utils/generate_metadata.js",
  },
  "dependencies": {
      "canvas": "^2.8.0",
      "dotenv": "^16.0.3",
      "gif-encoder-2": "^1.0.5",
      "ipfs-http-client": "^59.0.0",
      "sha1": "^1.1.1"
 }

I can run the npm run generate_metadata command just fine however whenever I try to add a require statement to const { ipfsClient } = require('ipfs-http-client'); to the file I get an error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\source\3_market_nft_engine\node_modules\ipfs-http-client\package.json at new NodeError (node:internal/errors:393:5) at throwExportsNotFound (node:internal/modules/esm/resolve:358:9) at packageExportsResolve (node:internal/modules/esm/resolve:612:7) at resolveExports (node:internal/modules/cjs/loader:529:36) at Module._findPath (node:internal/modules/cjs/loader:569:31) at Module._resolveFilename (node:internal/modules/cjs/loader:981:27) at Module._load (node:internal/modules/cjs/loader:841:27) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18) at Object. (C:\source\3_market_nft_engine\utils\generate_metadata.js:4:20) { code: 'ERR_PACKAGE_PATH_NOT_EXPORTED' }

I've tried:

  1. Changing my version of node
  2. Reinstalling the packages
  3. Using yarn instead
Ritzy Dev
  • 387
  • 3
  • 10

1 Answers1

1

This is because there is no main in that package's package.json (https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-http-client/package.json). Per this note in the changelog, the package is meant to be used with ESM, but that also wouldn't fix your problem because there's no export called ipfsClient; you need import { create } from 'ipfs-http-client'.

Edit: as noted in your comment, downgrading to a version from before the package switched to ESM also works. Details in the comment.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • 1
    Sorry, I'm relatively new to node development I just tried what you stated: `import { create } from 'ipfs-http-client'` and I also tried `const { create } = require('ipfs-http-client');` I'm getting the same error – Ritzy Dev Nov 04 '22 at 03:18
  • 1
    Did you add `type: module` to your package.json? Or if that's not doable in your code base, does using the async import method in the changelog linked above work for you? – Zac Anger Nov 04 '22 at 03:25
  • I'm pretty vanilla still I'm not sure what ESM is and type:module but ill do some research – Ritzy Dev Nov 04 '22 at 03:38
  • 1
    I got it working using `npm i ipfs-http-client@50.1.2 @babel /core --save` – Ritzy Dev Nov 04 '22 at 03:43
  • Not sure why it works but if you add it to your answer I'll mark it as correct or maybe you can help explain – Ritzy Dev Nov 04 '22 at 03:46
  • Downgrading the version worked because the older version used commonjs modules. I would recommend reading a bit about Ecmascript Modules vs CommonJS, but basically Node used to use its own system for modules (require/module.exports), and everyone's been gradually transitioning their packages to ESM (import/export) since around 2015 when it became a part of the language standard. – Zac Anger Nov 04 '22 at 03:53