1.安装node、npm

node以及npm都需要是最新版本(版本过低有坑)

2.安装淘宝镜像cnpm(建议,下载较快)

npm install -g cnpm --registry=https://registry.npm.taobao.org

3.安装electron

cnpm install -g electron

4.安装打包输出工具

cnpm install -g electron-packager

5.安装electron 客户端工具(选择性,其实没必要)

Electron.exe

链接:http://pan.baidu.com/s/1mieJnLI 密码:x2i8

安装完成双击electron.exe文件

6.新建一个文件夹,命名为electron,在文件夹目录下,创建三个文件,package.json,main.js,index.htmlpackage.json可以直接npm init生成

package.json文件:

{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"pack": "electron-packager . myFirstElectron --win --out ./dist --arch=x64 --app-version=0.0.1 --electron-version=9.0.5"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"sqlite3": "^4.2.0"
}
}

main.js文件:

const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron; // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected. let win; function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: { //If you add this sentence, you will not report an error
nodeIntegration: true
}
}); // and load the index.html of the app.
win.loadURL(`file://${__dirname}/index.html`); // Open the DevTools.
win.webContents.openDevTools(); // Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
} // This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow); // Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
}); app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
}); // In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
var sqlite3 = require('sqlite3').verbose();
const path = require('path');
var db = new sqlite3.Database(path.join(__dirname, 'db.db'));

index.html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>

7.运行electron

安装了客户端可以直接拖入
未安装就直接自定义下package.json,顺带把打包指令配置下
注意后面的版本--electron-version=9.0.5写你自己的enectron版本(cmd命令electron -v)

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"pack": "electron-packager . myFirstElectron --win --out ./dist --arch=x64 --app-version=0.0.1 --electron-version=9.0.5"
},

8.集成sqlite3数据库

为什么选择sqlite3?

  • Electron作为现今比较流行的客户端框架,势必会用本地缓存,在以往软件的一些缓存中一般用到的文件、日志等,这里提到的是sqlite3——轻量级数据库。
  • Electron是完全符合node.js语法,并且支持很多第三方库,sqlite3也是其中一块,使用它首先需要具备node.js环境,这里不再赘述,安装sqlite3:

npm install sqlite3 --save

安装以后,发现Electron不能正常使用,会报出很多错误,比如缺少sqlite3模块,找不到,但是明明装了,这里需要对Sqlite3单独编译,

原因是:通过npm安装的sqlite3模块只实现了对node.js原生环境的支持,如果electron需要使用的话必须对其进行二次编辑。

1.首先进入到安装好的模块sqlite3目录下

cd .\node_modules\sqlite3

2.安装nan,并run,如果run失败可以跳过

npm install nan --save
npm run prepublish

3.编译下(可能会出现报错)

node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.6.6-win32-ia32
node-gyp rebuild -target=1.6.6 -arch=win32 -target_platform=win32 -dist-url=https://atom.io/download/electron/ -module_name=node_sqlite3 -module_path=../lib/binding/electron-v1.6.6-win32-ia32

4.如果要修改electron的版本,直接修改下方图片标红处就可以了。

详细讲解请点击

node报错解决方案(在使用node指令时可能会报错)gyp ERR! stack Error: Could not find any Visual Studio installation

步骤一

npm install --global --production windows-build-tools

npm install -g node-gyp

步骤二

上面步骤一如果没有安装好python2.7,则安装下

npm install --python=python2.7

npm config set python python2.7

 

最新文章

  1. Markdown 图片助手-MarkdownPicPicker
  2. [Intel Edison开发板] 01、Edison开发板性能简述
  3. 关于Git和Github你不知道的十件事
  4. Unity2.0容器自动注册机制
  5. zoj3261 带权并查集
  6. shader 语言 【转】
  7. URAL 1008 - Image Encoding(bfs坑爹题)
  8. 一些基础的.net用法
  9. 访问权限系列一(public/private/protected/default):成员变量
  10. php随笔6-thinkphp OA系统 JS 实时显示当前时间
  11. Java基础--定时任务Timer(转载)
  12. eclipse 内存优化
  13. makefile 转载
  14. C++ Primer 笔记——语句
  15. 百度地图 JavaScript API
  16. html table标签
  17. 四则运算web最终版
  18. sqlserver--install/uninstall
  19. 问题记录 为ubuntu16.04添加windows字体(解决JIRA图表乱码的问题)
  20. 《opencv学习》 之 几何变换

热门文章

  1. Java实现蓝桥杯 九宫幻方
  2. Java实现 蓝桥杯算法提高金明的预算方案
  3. Java实现蓝桥杯正则切分
  4. 记 Centos zabbix-agent启动失败解决思路
  5. 什么?你还不会身份证号码验证?最全的身份证正则验证js
  6. VSCode最佳设置
  7. 【shell】十分钟轻松入门;如果没入门,您吐口口水再走吧!
  8. php的ts和nts选择
  9. 软光栅-uraster代码阅读(入门极品)
  10. Vue中hash模式和history模式的区别