官网
ROBOTJS官网: https://robotjs.io/docs/
ROBOTJS源码: https://github.com/octalmage/robotjs
NodeJS地址:https://nodejs.org/en/download/
Electron版本: https://electronjs.org/releases/stable
环境设置
Node版本不要用最新版本 用v10.16.3即可
更新npm的包镜像源
1 | npm config set registry https://registry.npm.taobao.org |
还原默认配置
1 | npm config set registry https://registry.npmjs.org |
注意:要用管理员身份运行CMD
1 | npm install -g --production windows-build-tools |
如果windows-build-tools删除下面的文件夹(根据自己系统的情况查找)
1 | C:\Users\Jian\AppData\Roaming\npm\node_modules\windows-build-tools |
重新安装
设置python路径
1 | npm config set python C:\Users\Jian\.windows-build-tools\python27\python.exe |
构建
下载编译依赖
添加robotjs依赖为Git仓库
1 | "dependencies": { |
安装依赖
1 | npm install |
打开项目下的node_modules
下的robotjs
文件夹
如果没有生成/build/Release/robotjs.node
就执行下面的操作
下载依赖
1 | cnpm install |
编译
1 | node-gyp rebuild --python C:\Users\Jian\.windows-build-tools\python27\python.exe |
构建的robotjs.node
的Node版本如果和Electron中的Node版本不一样的话就需要执行下面的rebuild操作
Rebuild方式一(官方)
查看系统的Node版本
1 | node -v |
获取Electron内部的node版本
在代码中打印信息
1 | console.info(process.versions); |
结果如下
1 | ares: "1.15.0" |
在Electron中使用需要重新构建
1 | npm rebuild --runtime=electron --target=1.1.3 --disturl=https://atom.io/download/atom-shell --abi=48 |
其中的
Target should be the Electron version, and abi should be the abi version of the included Node.
所以
1 | electron: "6.0.12" |
推断出
1 | --target=6.0.12 |
node和abi的对应关系:参见
但是
1 | node: "12.4.0" |
并没有在对应表里
1 | { |
但是我们发现每一个大的node版本对应的node_abi的版本都是一样的
所以
1 | --abi=72 |
整体的命令就是
1 | npm rebuild --runtime=electron --target=6.0.12 --disturl=https://atom.io/download/atom-shell --abi=72 |
当然也可以配置到 package.json 中
1 | "rebuild": "npm rebuild --runtime=electron --target=6.0.12 --disturl=https://atom.io/download/atom-shell --abi=72" |
至此 RobotJS就能正常使用了
Rebuild方式二(推荐)
安装electron-rebuild插件,能够方便遍历node-modules
中所有需要rebuild
的库进行重编译。
1 | npm install electron-rebuild --save |
在package.json中配置快捷方式
1 | "scripts": { |
或者
推荐 下面的方法能使用淘宝的镜像,防止构建时下载依赖失败
1 | "scripts": { |
-v 为Electron的版本号
之后执行
1 | npm run rebuild |
操作即可完成electron
的重编译。