electron搭建开发环境

 

环境:windons10, nodev14.17.1, vscode

 

md a_star

cd a_star

npm i -g yarn

yarn config set ELECTRON_MIRROR https://cdn.npm.taobao.org/dist/electron/

因为在此系统上禁止运行脚本...报错时,执行下面命令
set-ExecutionPolicy RemoteSigned

 

yarn init

yarn config set ELECTRON_MIRROR https://cdn.npm.taobao.org/dist/electron/

yarn add electron@8.1.0 --dev

yarn add electron@8.1.0 --dev --platform=win64

 

新建页面index.html

<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.

    <!-- You can also require other files to run in this process -->
    <script src="./render.js"></script>
  </body>
</html>

index.js

var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var win = null;
app.on('ready', function(){
    win = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
    win.loadFile('index.html');
    win.on('closed', function(){
        win = null
    });
}
);

app.on('window-all-closed', function () {
    console.log(process.platform);
    if (process.platform !== 'darwin') app.quit()
});
  

 

 package.json

{
  "name": "a_star",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "electron ./index.js"
  },
  "license": "MIT",
  "devDependencies": {
    "electron": "8.1.0"
  }
}

 

yarn start 

如果启动失败,确认一下下面的目录是否存在,如果不存在则说明安装不成功,错误信息中也会提示你重试,那就重试,本人是第二次重试时安装成功

node_modules/electron/dist/

 

 

posted @ 2021-06-06 21:52  方诚  阅读(223)  评论(0编辑  收藏  举报