在Windows10上运行DAPP示例


#1
  1. git
    git is a free and open source distributed version control system
    git 是比较底层的软件,有对它的包装后的软件,如Git.(带GUI界面,也包括Git Bash是命令行窗口)

2)Dapp尝试:
准备工作:
¬ 下载了(https://gitforwindows.org/ ) v2.18.0
¬ 另外我还下载了visual Studio code 并安装, 然后再安装git(选中code阅读工具为vs code)
¬ 在git GUI工具clone 了这个:https://github.com/CryptapeHackathon/Hackthon-web3-neuron-example.git
获得例子的源代码。
¬ 在vs code 中打开本地代码的folder
¬ 用remix 编写、调试、编译solidity智能合约,得到bin code, abi
¬ 部署合约:想尝试用web3.js来部署,需要先下载web3
¬ 先需要安装npm https://docs.npmjs.com/cli/npm
npm is the package manager for JavaScript and the world’s largest software registry.
node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It bundles the gyp project used by the Chromium team and takes away the pain of dealing with the various differences in build platforms.

Windows上如何使用npm ? 在装Node.js 时候选择这个就好了

我已经装好了吗? Check in Node.js command prompt:


确实已经装好了。

然后尝试web3j readme 里面的命令yarn add @nervos/web3
发现出错,所以先用npm 安装yarn: npm install yarn

发现还是不行,yarn 命令还是不认识。
于是找到https://yarnpkg.com/en/docs/install#windows-stable, 按页面指示下载yarn安装(download installer). 页面提示:If you use the installer you will first need to install Node.js.
下载完后,安装Install via Scoop,所以又得先装scoop,按照
https://github.com/lukesampson/scoop/wiki/Quick-Start 这个页面指示安装scoop:

最后在上面这个窗口再输入命令:scoop install yarn
安装yarn。提示安装成功。然后再在这个窗口安装web3,即输入yarn add @nervos/web3 ,还是失败了(失败原因,没有在这个shell里成功翻墙!)

于是又去Node.js command prompt 中尝试yarn add @nervos/web3, 一开始还不认识yarn,于是重启。再输入,认识了,但是执行失败(Can’t find Python executable “python”, you can set the PYTHON env variable.),截图如下:

原因可能是(最后发现不是!)node-gyp没有安装(验证是否没有安装node-gyp:输入node-gyp –v返回错误),然后根据 https://github.com/nodejs/node-gyp#installation安装node-gyp, 即在Node.js command prompt中输入:npm install -g node-gyp
还是不行,再装一次:在cmd窗口中输入:npm install --global --production windows-build-tools,
还是不行,提示说需要用Administrator身份运行此命令。
解决办法:
(参考
https://www.top-password.com/blog/5-ways-to-run-powershell-as-administrator-in-windows-10/)在window10的坐下角搜索中输入powershell,然后右键点击,并选择“以管理员身份运行powershell”.
终于成功了:

所以原来是一开始的npm没有安装好,gitGUI的锅吗。

现在npm-gyp也装好了,可是在node.js command prompt中执行yarn 那条命令安装web3,仍然出错:

然后https://github.com/nodejs/node-gyp#installation 这个网页里看到一句:
If you have multiple Python versions installed, you can identify which Python version node-gyp uses by setting the ‘–python’ variable:
node-gyp --python /path/to/python2.7 试过不work! 于是我再试: If node-gyp is called by way of npm, and you have multiple versions of Python installed, then you can set npm's 'python' config key to the appropriate value: npm config set python /path/to/executable/python2.7
(再不work我就要跪下了)

然后,还是出错:
Can’t find Python executable “/path/to/executable/python2.7”, you can set the PYTHON env variable
跪下也没有用。继续搜,尝试
先装python2.7, then run : npm config set python python2.7
并在系统的环境变量的path中添加了python文件夹地址
还是找不到。
于是,试:npm config set python D:\Program Files\python\python.exe

真是一场噩梦啊。。。
还是不对,因为有空格!重新执行命令:
npm config set python “D:\Program Files\python\python.exe”
终于不再出找不到python的错了。但是有别的错误。

墙没翻出去。。。问题似曾相识。这就是前面在powershell没成功的原因。
然后现在powershell 里面已经不认识python了(“D:\Program Files\python\python.exe”之前没输对)重打之后就OK。
翻墙:
set http_proxy=http://127.0.0.1:1080;
set https_proxy=http://127.0.0.1:1080
最后 yarn 命令安装web3终于成功。

image

截图留念。

此时,跑node contract_deploy.js (里面有一行:const NervosWeb3 = require(’@nervos/chain’);)
报错:找不到 @nervos/chain
虽然我用yarn安装了@nervos/chain,但是node.js并没有找到这个module。
原因:

  1. Node.JS looks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading modules.
  2. If the given module is not a core module (e.g. http, fs, etc.), Node.js will then begin to search for a directory named, node_modules. It will start in the current directory (relative to the currently-executing file in Node.JS) and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the node_modules folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for example
  3. If you make a request to load the module, “utils” and its a directory not a .js file then: Node.JS will search a hierarchical directory for node_modules and utils in the following ways: ./node_modules/utils.js ./node_modules/utils/index.js ./node_modules/utils/package.json
  4. If Node.JS still can’t find the file in above steps, Node.js will then start to look into the directory paths from environment variables i.e. NODE_PATH set on your machine(obviously set by Node.JS installer file if you are on windows) Not Found in all the above steps then, prints a stack trace to stder E.g.: Error:Cannot find module ‘yourfile’ For more information: link is here even the cyclic require() is explained very well.
    原来之前的nervos\chain 装在了C:\Users\xiangmei\node_modules@nervos,而这个Node命令式在D盘的,所以运行的时候没找到node_module文件夹里有它,于是cd 到D:\Program Files\nodejs下重新yarn add nervos那个命令安装,再次安装。最后在安装在这里了。
    D:\Program Files\nodejs\node_modules@nervos

然后我把整个代码的文件夹移到了D:\Program Files\nodejs\GitRepo\AppChainDoc-ex\tar
运行就能找到module了。