【问题标题】:Unable to install web3 js for windows10windows10无法安装web3 js
【发布时间】:2019-06-06 16:12:38
【问题描述】:

问题是windows10无法安装web3 js。我尝试使用以下命令安装最新版本的 Web3 js:

npm install --save web3@1.0.0-beta.46

终端错误:

PS C:\Users\RSky> npm install --save web3@1.0.0-beta.46

> scrypt@6.0.3 preinstall C:\Users\RSky\node_modules\scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install C:\Users\RSky\node_modules\scrypt
> node-gyp rebuild


C:\Users\RSky\node_modules\scrypt>if not defined npm_config_node_gyp (node "C:\Users\RSky\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Users\RSky\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
internal/modules/cjs/loader.js:584
    throw err;
    ^

Error: Cannot find module 'nan'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at [eval]:1:1
    at Script.runInThisContext (vm.js:119:20)
    at Object.runInThisContext (vm.js:326:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at evalScript (internal/bootstrap/node.js:589:27)
gyp: Call to 'node -e "require('nan')"' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (C:\Users\RSky\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\configure.js:345:16)
gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\RSky\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\RSky\node_modules\scrypt
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\RSky\package.json'
npm WARN RSky No description
npm WARN RSky No repository field.
npm WARN RSky No README data
npm WARN RSky No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\RSky\AppData\Roaming\npm-cache\_logs\2019-03-05T18_04_48_554Z-debug.log

这个错误的原因是什么?

【问题讨论】:

    标签: solidity smartcontracts web3


    【解决方案1】:

    你有合同

        constructor() public {
            owner == msg.sender;
        }
    

    对吗?

    不应该

        constructor() public {
            owner = msg.sender;
        }
    

    更新 1:

        // Web3.js instance
        window.web3 = new Web3(web3.currentProvider);
    

    你正在写web3.currentProvider。但是在您的代码中 web3 没有在任何地方声明。是在head中声明的吗?

    根据您的代码,我了解到您没有使用metamask。如果我们使用元掩码,它会将 web3 库的一个版本注入到页面中。我们可以使用它来获取一个 web3 实例,例如

        var web3 = new Web3(window.web3.currentProvider);
    

    但如果我们不使用元掩码,我们需要使用 3rd 方提供者来获取 web3 实例,如 infura 并且上面的代码行将更改为

        var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/<<your infura token>>"));
    

    更新 2:

    改变

        // Contract instance
        window.contractInstance = new window.web3.eth.contract(abi,myContractAddress)
    
        contractInstance.owner((err, ownerValue) => {
            document.querySelector('#owner-address').innerHTML(ownerValue)
        })
    

        // Contract instance
        window.contractInstance = window.web3.eth.contract(abi).at(myContractAddress)
    
        contractInstance.owner((err, ownerValue) => {
            document.querySelector('#owner-address').innerHTML = ownerValue;
        })
    

    我在 Rinkeby 测试网及其工作上部署了相同的合约。合约地址0x104ec30CD6FDC5889eDff672b7ac8a2a42232F64

    更新 3:

    问题已被编辑,现在这个答案与问题不符。

    【讨论】:

    • 是的,但在 Chrome 中“contractInstance.owner 不是一个函数”并没有解决这个问题,在 Microsoft Edge 中,它一直告诉我“'web3' is not defined”
    • 我正在使用 MetaMask Ropsten 测试网络,我得到的所有交易都非常好,因为它给出了 myContractAddress = "0x50c57b9eb3c4fe581016f1e5d507e3daba3fd26d" 所以我对此没有任何问题。我的 Web3.js 声明为 你可以在
    【解决方案2】:

    替换

    window.contractInstance = new web3.eth.contract(abi, myContractAddress)
    

    window.contractInstance = new window.web3.eth.contract(abi, myContractAddress)
    

    【讨论】:

    • 请用您的配置、输入、预期和实际输出更新问题。
    • 不幸的是,这行新代码到目前为止没有帮助,所以我现在有点迷失了同样的错误。
    猜你喜欢
    • 2022-06-14
    • 2018-07-11
    • 1970-01-01
    • 2021-07-28
    • 2018-02-05
    • 2019-01-08
    • 2019-07-30
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多