npm安装cnpm、ganache-cli、truffle等后,使用它们时提示Command not found。需添加环境变量

准备学习solidity,按照书本要求在linux上搭建环境,先后安装了nodejs、npm等
node -v

v15.8.0

npm -v

7.16.0

然后使用npm安装cnpm,执行
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
之后使用cnpm安装web3,执行
sudo cnpm install web3 -g
提示:

Command ‘cnpm’ not found

输入cnpm -v也是如此。
于是打算跳过,进行下一步用npm安装ganache-cli和truffle,执行
sudo npm install -g ganache-cli
安装后显示:

changed 6 packages in 1s

2 packages are looking for funding run ‘npm fund’ for details

执行
sudo npm install -g truffle
安装完成后提示了一些warning:

npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated fsevents@2.1.3: “Please update to latest v2.3 or v2.2”
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated debug@4.1.1: Debug versions >=3.2.0 < 3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
npm WARN deprecated remotedev-serialize@0.1.9: Package moved to @redux-devtools/serialize.
npm WARN deprecated redux-devtools-instrument@1.10.0: Package moved to @redux-devtools/instrument.
npm WARN deprecated redux-devtools-core@0.2.1: Package moved to @redux-devtools/app.
npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated multicodec@0.5.7: stable api reached
npm WARN deprecated node-pre-gyp@0.11.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future
npm WARN deprecated ethereumjs-common@1.5.2: New versions are published within new scoped package name format @ethereumjs/common. Please update.
npm WARN deprecated core-js@2.6.12: core-js@< 3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.

changed 886 packages in 2m

此时无法使用ganache和truffle,同样提示Command not found,怀疑是否安装成功
查看npm所有全局安装的模块,执行npm list -g

/usr/local/node/node-v15.8.0-linux-x64/lib
├── cnpm@6.2.0
├── ethereumjs-testrpc@6.0.3
├── ganache-cli@6.12.2
├── npm@7.16.0
├── solc@0.8.4
├── truffle@5.3.9
├── web3@1.3.6
└── yarn@1.22.10

应该都已经安装成功

最终发现是未设置环境变量所致
解决方法为当前用户永久添加环境变量

1、先要找到cnpm、truffle等的具体路径,可使用whereis命令查找,比如执行whereis truffle后显示:

truffle: /usr/local/node/node-v15.8.0-linux-x64/bin/truffle

2、编辑用户目录下的隐藏文件.bashrc(执行ls看不到,需要执行ls -a
在用户目录下执行vim .bashrc进入编辑

在文件末尾添加export PATH="/usr/local/node/node-v15.8.0-linux-x64/bin/:$PATH"
注意:具体的path路径大家可能不同
3、保存退出后执行命令source ~/.bashrc使之生效
成功解决!

首先,你安装 Node.js,可以在官网下载对应的版本:https://nodejs.org/en/download/ 接着,可以使用 npm(Node.js 的包管理工具)安装 Ganache 和 Web3.js: ``` npm install -g ganache-cli web3 ``` 安装完成后,可以在命令行中执行以下命令启动 Ganache: ``` ganache-cli ``` 这会在本地启动一个以太坊仿真节点,你可以在浏览器中访问 http://localhost:8545 来查看节点信息。 接下来,可以使用 Web3.js 连接到 Ganache 节点,并在其中部署和测试 Solidity 智能合约。例如,可以使用以下代码连接到 Ganache 节点: ``` const Web3 = require('web3'); const web3 = new Web3('http://localhost:8545'); ``` 然后,可以使用 web3.js 提供的接口编写 Solidity 智能合约,并使用以下代码将其部署到 Ganache 节点上: ``` const fs = require('fs'); const solc = require('solc'); const contractSource = fs.readFileSync('MyContract.sol', 'utf8'); const compiledContract = solc.compile(contractSource, 1); const contractABI = JSON.parse(compiledContract.contracts[':MyContract'].interface); const contractBytecode = compiledContract.contracts[':MyContract'].bytecode; const MyContract = new web3.eth.Contract(contractABI); MyContract.deploy({ data: contractBytecode, arguments: [123, 'hello'] }).send({ from: '0x123...', gas: 1500000, gasPrice: '30000000000000' }).then((newContractInstance) => { console.log(newContractInstance.options.address); }); ``` 这会将编写的 MyContract 合约部署到 Ganache 节点上,并输出新合约实例的地址。 最后,可以使用 Web3.js 提供的接口与部署的合约进行交互,例如: ``` MyContract.methods.myFunction().call().then((result) => { console.log(result); }); ``` 这会调用 MyContract 合约中的 myFunction 函数,并输出返回结果。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值