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數據庫
- 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
