0Pricing
Electron Desktop App Development · 课时

使用原生模块

学习如何在 Electron 应用中集成并使用现有的 Node.js 原生模块(C/C++),以执行对性能要求较高的任务

使用原生模块 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Native Modules?

Welcome! Today we'll explore Native Modules in Electron. These are special Node.js modules written in C or C++.

Think of them as powerful extensions that let your JavaScript code interact directly with the operating system at a lower level or perform high-speed computations.

Why Use Native Modules?

Native modules are used for specific, critical tasks:

  • Performance: For CPU-intensive operations that JavaScript might struggle with.
  • Low-level OS Access: Interacting with hardware or specific operating system features not exposed by Node.js or Electron's standard APIs.
  • Existing C/C++ Libraries: Reusing established, optimized C/C++ libraries directly within your Electron app.

Node.js Addons in Electron

Electron uses Node.js internally. This means any Node.js native addon (a C/C++ module compiled for Node.js) can also be used in your Electron application.

You'll typically load these modules in the main process, as it has full Node.js API access, and then expose necessary functions to the renderer process via IPC or preload scripts (covered in later lessons).

Prerequisites for Building

To build native modules, you need a few tools installed on your system:

  • Python: Version 2.7 (for node-gyp).
  • C/C++ Compiler: Like Visual Studio (Windows), Xcode Command Line Tools (macOS), or build-essential (Linux).
  • node-gyp: A cross-platform command-line tool written in Node.js for compiling native addon modules.

`node-gyp` Explained

node-gyp is crucial for native module development. It takes configuration files (binding.gyp) and uses them to generate platform-specific build files (like Makefiles or Visual Studio projects).

This allows your C/C++ code to be compiled correctly for your operating system and Node.js version, producing a .node file that Node.js (and Electron) can load.

Basic Structure (Concept)

A native module project typically includes:

  • C/C++ source files: Your core logic.
  • binding.gyp: A configuration file for node-gyp, specifying how to compile your C/C++ code.
  • package.json: Defines your module and its dependencies.

The compilation process turns your C/C++ code into a .node file, which is essentially a shared library.

Installation & Compilation

When you install an npm package that contains a native module, npm automatically runs node-gyp rebuild for you.

This command compiles the native code for your specific Node.js and Electron version, creating the .node file in a directory like ./build/Release/ or ./build/Debug/.

Loading in Electron

Once compiled, a native module can be loaded into your Electron application using the standard Node.js require() function.

You simply point to the compiled .node file. This is usually done in your Electron's main process, or in a preload script for the renderer process if you need to expose its functionality there securely.

Using a Native Module (JS Example)

Here's how you would use a hypothetical native module in your Electron application's JavaScript code. We'll simulate its functionality for this runnable example.

const path = require('path');

// In a real Electron app, you'd require the compiled .node file.
// Example: const myNativeAddon = require(path.join(__dirname, 'build/Release/myaddon.node'));

// For this runnable example, we'll simulate a native module interface.
const myNativeAddon = {
  greet: (name) => {
    return `Hello from native code, ${name}!`;
  },
  add: (a, b) => {
    console.log('Performing fast native addition...');
    return a + b;
  }
};

console.log("Electron app script running...");

// Use the functions exposed by the 'native' module
const greeting = myNativeAddon.greet("CoddyKit User");
console.log(greeting);

const sum = myNativeAddon.add(15, 25);
console.log(`The sum of 15 and 25 is: ${sum}`);

console.log("Script finished.");

Quick Check

Which of the following is NOT a primary reason to use Electron native modules?

Recap & Next Steps

Great job! You've learned about Node.js native modules in Electron:

  • They are C/C++ code compiled to .node files.
  • Used for performance, OS interaction, and C/C++ library integration.
  • node-gyp is the key tool for compilation.
  • They are loaded using require() in JavaScript.

Next, we'll look at creating Tray Applications and Badges, another way to leverage native features!

常见问题解答

「使用原生模块」课时是免费的吗?

是的 — 「使用原生模块」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。

「使用原生模块」这节课中我会学到什么?

学习如何在 Electron 应用中集成并使用现有的 Node.js 原生模块(C/C++),以执行对性能要求较高的任务 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Electron Desktop App Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「使用原生模块」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Electron Desktop App Development 课中编写并运行代码吗?

能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用原生模块
  2. 托盘应用与徽章
  3. 屏幕捕获与媒体
  4. 电源与硬件监控
← 返回 Electron Desktop App Development