import javascript module: The requested module X does not provide an export named Y

huangapple go评论54阅读模式
英文:

import javascript module: The requested module X does not provide an export named Y

问题

以下是已经翻译好的代码部分:

import IPFS from 'ipfs'
import OrbitDB from 'orbit-db'

;(async function () {
  const ipfs = await IPFS.create()
  const orbitdb = await OrbitDB.createInstance(ipfs)

  // 创建/打开数据库
  const db = await orbitdb.log("hello")
  await db.load()

  // 监听来自对等节点的更新
  db.events.on("replicated", address => {
    console.log(db.iterator({ limit: -1 }).collect())
  })

  // 添加一个条目
  const hash = await db.add("world")
  console.log(hash)

  // 查询
  const result = db.iterator({ limit: -1 }).collect()
  console.log(JSON.stringify(result, null, 2))
})()

希望这有助于解决你的问题。如果你有任何其他问题,请随时提出。

英文:

I am having a problem to import some of the content of a JavaScript module (ipfs). Here is the code (test.js), taken from OrbitDB Module with IPFS Instance that I am trying to run from the command line as a node app (node test.js):

import IPFS from 'ipfs'
import OrbitDB from 'orbit-db'

;(async function () {
  const ipfs = await IPFS.create()
  const orbitdb = await OrbitDB.createInstance(ipfs)

  // Create / Open a database
  const db = await orbitdb.log("hello")
  await db.load()

  // Listen for updates from peers
  db.events.on("replicated", address => {
    console.log(db.iterator({ limit: -1 }).collect())
  })

  // Add an entry
  const hash = await db.add("world")
  console.log(hash)

  // Query
  const result = db.iterator({ limit: -1 }).collect()
  console.log(JSON.stringify(result, null, 2))
})()

When I run this code from the command line with node, it throws the following error:

import IPFS from 'ipfs'
       ^^^^
SyntaxError: The requested module 'ipfs' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:127:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:193:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

I tried to find out what is wrong here. So I ctrl-clicked on ipfs and it takes me to the following index.d.ts:

/**
 * @typedef {import('ipfs-core-types').IPFS} IPFS
 */
export const create: typeof import("ipfs-core/src/components").create;
export const crypto: typeof import("libp2p-crypto");
export const isIPFS: typeof import("is-ipfs");
export const CID: typeof import("multiformats").CID;
export const multiaddr: typeof import("multiaddr").Multiaddr;
export const PeerId: typeof import("peer-id");
export const globSource: typeof import("ipfs-utils/src/files/glob-source");
export const urlSource: typeof import("ipfs-utils/src/files/url-source");
export const path: typeof pathImport;
export type IPFS = import('ipfs-core-types').IPFS;
import { path as pathImport } from "./path.js";
//# sourceMappingURL=index.d.ts.map

It looks to me like IPFS is exported by a named export. So I tried to change my code to named import, like this:

import {IPFS} from 'ipfs'

This throws the following error:

import {IPFS} from 'ipfs'
        ^^^^
SyntaxError: The requested module 'ipfs' does not provide an export named 'IPFS'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:127:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:193:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

So how can I solve this problem?

Here is the package.json that I have together with test.js:

{
  "name": "ipfs-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",  
  "type": "module"
}

I should mention that I wrote the code in Intellij, and it did not show a compilation error, which it usually does when it cannot locate imports. So that's also weird.

答案1

得分: 0

以下是翻译好的部分:

  1. There is no default export (export default somethingSomething) so using import IPFS from 'ipfs' won't work.

    • 没有默认导出 (export default somethingSomething),因此使用 import IPFS from 'ipfs' 不会起作用。
  2. You are half-right that a named export IPFS is there, however you looked at a TypeScript definition file, and the export named IPFS is just a type in TypeScript and not anything JavaScript even sees.

    • 你说得有一半对,有一个名为 IPFS 的命名导出,但是你看的是一个 TypeScript 定义文件,而且名为 IPFS 的导出仅仅是 TypeScript 中的一个 类型,JavaScript 根本看不到它。
  3. Instead, you can see that the create method you used in IPFS.create is a named export, so import { create } from 'ipfs' would work, but in fact you can keep your code that uses IPFS.create and simply change import IPFS from 'ipfs' to import * as IPFS from 'ipfs'!

    • 相反,你可以看到你在 IPFS.create 中使用的 create 方法是一个命名导出,所以 import { create } from 'ipfs' 会起作用,但事实上,你可以保留使用 IPFS.create 的代码,只需将 import IPFS from 'ipfs' 更改为 import * as IPFS from 'ipfs'
  4. The import * as something from 'something' syntax will give you an object with all named exports inside.

    • import * as something from 'something' 语法会给你一个包含所有命名导出的对象。
英文:

There is no default export (export default somethingSomething) so using import IPFS from 'ipfs' won't work.

You are half-right that a named export IPFS is there, however you looked at a TypeScript definition file, and the export named IPFS is just a type in TypeScript and not anything JavaScript even sees.

Instead, you can see that the create method you used in IPFS.create is a named export, so import { create } from 'ipfs' would work, but in fact you can keep your code that uses IPFS.create and simply change import IPFS from 'ipfs' to import * as IPFS from 'ipfs'!

The import * as something from 'something' syntax will give you an object with all named exports inside.

huangapple
  • 本文由 发表于 2023年5月18日 06:46:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定