简体   繁体   中英

Unable to install web3 js for windows10

The problem is to unable to install web3 js for windows10. I tried to install the latest version of Web3 js using the command:

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

The error in terminal:

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

What the cause of that error?

Replace

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

With

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

in contract you have

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

is it correct?

shouldn't it be

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

UPDATE 1:

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

you are writing web3.currentProvider . But in your code web3 is not declared anywhere. Is it declared in head ?

From your code I understand that you are not using metamask . If we use metamask it injects a version of web3 library into a page. We can use it to get a web3 instance like

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

But if we are not using metamask we need to use a 3rd party provider to get the web3 instance like infura and the above line of code will change to

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

UPDATE 2:

change

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

    contractInstance.owner((err, ownerValue) => {
        document.querySelector('#owner-address').innerHTML(ownerValue)
    })

to

    // Contract instance
    window.contractInstance = window.web3.eth.contract(abi).at(myContractAddress)

    contractInstance.owner((err, ownerValue) => {
        document.querySelector('#owner-address').innerHTML = ownerValue;
    })

I deployed same contract on Rinkeby test net and its working. Contract address 0x104ec30CD6FDC5889eDff672b7ac8a2a42232F64

UPDATE 3:

Question was edited and now this answer doesn't go with the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM