Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Electron Installation

Electron Installation

Before installing Electron, you need to prepare two basic environments:

1. Node.js

Electron is a framework based on Node.js, so you must install Node.js first.

Check whether it is installed:

node -v

npm -v

If a version number is output, it is installed.
If not, please go to the Node.js official website Download LTS (Long-Term Support version).

After installation, npm (Node package manager) is included automatically.

If you are not familiar with Node.js, refer to: Node.js

the tutorial.

2. npm or yarn

Installing Electron relies on npm or yarn for package management.

You can choose:



# 使用 npm

npm install electron --save-dev



# or use yarn

yarn add electron --dev


Install Electron

There are two ways to install Electron:Global installation and Project-local installation.
it is recommended to use Project-local installation(safer and easier to maintain).

1. Project-local installation

# 创建一个项目文件夹

mkdir my-electron-app

cd my-electron-app



# Initialize Node project

npm init -y



# Install Electron

npm install electron --save-dev

After a successful install, the following appears in the project directory:

node_modules/

package.json

package-lock.json


Verify whether the installation succeeded

Create a simple Electron startup file:

main.js

Example

const

{

app

,

BrowserWindow

}

=

require

(

'electron'

)

function

createWindow

(

)

{

 

const

win

=

new

BrowserWindow

(

{

    width

:

800

,

    height

:

600

,

 

}

)

  win.

loadFile

(

'index.html'

)

}

app.

whenReady

(

)

.

then

(

(

)

=>

{

  createWindow

(

)

}

)

index.html

Example

DOCTYPE html

DOCTYPE html

<

html

<

head

<

meta charset

=

"utf-8"

<

title

Runoob Tutorial

(

runoob.

com

)

title

head

<body>
<h1 Hello,Electron>

!

h1

   

<

p

RUNOOB Electron test.

p

body

html

Then add a start command in package.json:

"scripts": {

  "start": "electron ."

}

Run:

npm start

If a desktop window pops up saying "Hello, Electron!", the installation succeeded/p>


Common installation problems and solutions

Problem Reason Solution
npm ERR! code ELIFECYCLE Permission or cache issues Try running npm cache clean --force
electron failed to install correctly Network or download source timeout Install using a Taobao mirror (as below)

Use a domestic mirror to speed up installation

The Electron download package is large (tens of MB) and may time out domestically, so you can use a Taobao mirror:

# 临时设置

ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/ npm install electron --save-dev

Or permanently change the npm registry:

npm config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/


Optional tools

Tool name Purpose Install Command
electron-builder Package into an installer (.exe / .dmg, etc.) npm install electron-builder --save-dev
electron-reload Automatically reload after code changes npm install electron-reload --save-dev
electron-forge Quickly build templates npx create-electron-app my-app