0

我无法使用节点模块 ipfs.js
控制台显示错误:“找不到模块 'ipfs'”

Ubuntu 16.04.4 LTS

节点 --version == v8.10.0
npm --version == 5.6.0
ipfs 版本 == 0.4.13

我的 package.json:

{
  "scripts": {
    "dev": "lite-server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "lite-server": "^2.3.0"
  },
  "dependencies": {
    "ipfs": "^0.28.2",
    "web3": "^0.20.6"
  }
}

我的 app.js:

const IPFS = require('ipfs')
const node = new IPFS()
// // set the provider you want from Web3.providers
web3 = new Web3(new `Web3.providers.HttpProvider("http://127.0.0.1:8545"));`

当我在命令行中运行时,它的工作原理:

> const IPFS = require('ipfs')
undefined
> const node = new IPFS()
undefined
> Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmYwqrDJCQEiY2fijnwpPhhsG5w8rVxCTjK7duxtPyt24J
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmYwqrDJCQEiY2fijnwpPhhsG5w8rVxCTjK7duxtPyt24J
Swarm listening on /ip4/192.168.2.103/tcp/4002/ipfs/QmYwqrDJCQEiY2fijnwpPhhsG5w8rVxCTjK7duxtPyt24J
4

1 回答 1

0

我能够解决我的问题。我没有使用节点的经验,所以我完全混淆了它的使用,在服务器端和客户端使用的问题上。我的意图是在浏览器中使用 ipfs

我正在使用 Truffle 框架创建一个 Dapp,并且 truffle 提供了一个“web3.min.js”文件,这个库与“var ipfs = require ('ipfs').

解决方案很简单,我只在浏览器中使用 js-ipfs 库:

https://github.com/ipfs/js-ipfs#use-in-the-browser

现在我的代码是:

我的html:

<script src="js/web3.min.js"></script>
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
<script src="js/myjs.js"></script>

我的 app.js:

var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
console.log(web3);
console.log(Ipfs);
于 2018-03-19T17:10:01.620 回答