从零开始用electron整个跨平台桌面应用---基础配置篇

时间:2024-01-27 07:03:54


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