# 簡介
所有的[Node.js's built-in modules][1]在Electron中都可用,并且所有的node的第三方組件也可以放心使用(包括[自身的模塊][2])。
Electron也提供了一些額外的內置組件來開發傳統桌面應用。一些組件只可以在主進程中使用,一些只可以在渲染進程中使用,但是也有部分可以在這2種進程中都可使用。
基本規則:GUI模塊或者系統底層的模塊只可以在主進程中使用。要使用這些模塊,你應當很熟悉[主進程vs渲染進程][3]腳本的概念。
主進程腳本看起來像個普通的nodejs腳本
```javascript
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var window = null;
app.on('ready', function() {
window = new BrowserWindow({width: 800, height: 600});
window.loadURL('https://github.com');
});
```
渲染進程和傳統的web界面一樣,除了它具有使用node模塊的能力:
```html
<!DOCTYPE html>
<html>
<body>
<script>
const remote = require('electron').remote;
console.log(remote.app.getVersion());
</script>
</body>
</html>
```
如果想運行應用,參考 `Run your app` 。
## 解構任務
如果你使用的是CoffeeScript或Babel,你可以使用[destructuring assignment][4]來讓使用內置模塊更簡單:
```javascript
const {app, BrowserWindow} = require('electron');
```
然而如果你使用的是普通的JavaScript,你就需要等到Chrome支持ES6了。
##使用內置模塊時禁用舊樣式
在版本v0.35.0之前,所有的內置模塊都需要按造 `require('module-name')` 形式來使用,雖然它有很多[弊端][5],我們仍然在老的應用中友好的支持它。
為了完整的禁用舊樣式,你可以設置環境變量 `ELECTRON_HIDE_INTERNAL_MODULES ` :
```javascript
process.env.ELECTRON_HIDE_INTERNAL_MODULES = 'true'
```
或者調用 `hideInternalModules` API:
```javascript
require('electron').hideInternalModules()
```
[1]:http://nodejs.org/api/
[2]:https://github.com/heyunjiang/electron/blob/master/docs/tutorial/using-native-node-modules.md
[3]:https://github.com/heyunjiang/electron/blob/master/docs/tutorial/quick-start.md#the-main-process
[4]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
[5]:https://github.com/electron/electron/issues/387
- 介紹
- 常見問題
- Electron 常見問題
- 向導
- 支持平臺
- 分發應用
- 提交應用到 Mac App Store
- 打包應用
- 使用 Node 原生模塊
- 主進程調試
- 使用 Selenium 和 WebDriver
- 使用開發人員工具擴展
- 使用 Pepper Flash 插件
- 使用 Widevine CDM 插件
- 教程
- 快速入門
- 桌面環境集成
- 在線/離線事件探測
- API文檔
- 簡介
- 進程對象
- 支持的 Chrome 命令行開關
- 環境變量
- 自定義的 DOM 元素
- File 對象
- &lt;webview&gt; 標簽
- window.open 函數
- 在主進程內可用的模塊
- app
- autoUpdater
- BrowserWindow
- contentTracing
- dialog
- globalShortcut
- ipcMain
- Menu
- MenuItem
- powerMonitor
- powerSaveBlocker
- protocol
- session
- webContents
- Tray
- 在渲染進程(網頁)內可用的模塊
- desktopCapturer
- ipcRenderer
- remote
- webFrame
- 在兩種進程中都可用的模塊
- clipboard
- crashReporter
- nativeImage
- screen
- shell
- 開發
- 代碼規范
- 源碼目錄結構
- 與 NW.js(原 node-webkit)在技術上的差異
- 構建系統概覽
- 構建步驟(OS X)
- 構建步驟(Windows)
- 構建步驟(Linux)
- 在調試中使用 Symbol Server