IPFS模块导入报错:请求模块无对应导出项的解决问询
IPFS模块导入错误及解决方法
问题详情
导入ipfs模块时出现导出错误,使用OrbitDB官方文档提供的代码运行Node.js应用时失败:
原代码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)) })()
第一次运行错误
执行node test.js后抛出:
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/process/esm_loader:61:12) Process finished with exit code 1
修改导入方式后的错误
尝试改为命名导入import {IPFS} from 'ipfs',仍报错:
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/process/esm_loader:61:12) Process finished with exit code 1
当前环境配置
package.json内容:
{ "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" }
另外,IntelliJ编写代码时未提示编译错误,导致排查困难。
解决方法
1. 适配新版IPFS的导入方式
IPFS v0.60+版本重构了API,不再导出IPFS类,而是提供create函数作为命名导出。调整代码如下:
修改后的test.js:
import { create as createIPFS } from 'ipfs' import OrbitDB from 'orbit-db' ;(async function () { const ipfs = await createIPFS() 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)) })()
2. 解决IDE无提示错误的问题
- 执行
npm install重新安装依赖,确保类型文件与实际包版本一致 - 清理IDE缓存(IntelliJ路径:
File → Invalidate Caches...) - 安装对应类型包:
npm install @types/ipfs --save-dev
3. 降级IPFS到兼容版本
如果不想调整代码,可以降级IPFS到OrbitDB文档适配的旧版本(如v0.59.0):
npm uninstall ipfs npm install ipfs@0.59.0
降级后即可使用原文档中的import IPFS from 'ipfs'和IPFS.create()写法。
内容的提问来源于stack exchange,提问作者rapt
相关产品推荐
相关产品推荐

