0Pricing
Electron Desktop App Development · 강의

네이티브 모듈 사용

성능이 중요한 작업을 위해 기존 Node.js 네이티브 모듈(C/C++)을 Electron 애플리케이션에 통합하고 사용하는 방법을 배웁니다.

네이티브 모듈 사용은(는) CoddyKit의 무료 Electron Desktop App Development 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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!

자주 묻는 질문

“네이티브 모듈 사용” 강의는 무료인가요?

네 — “네이티브 모듈 사용” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Electron Desktop App Development 강의 전체를 잠금 해제할 수 있습니다. Electron Desktop App Development 강의에는 총 4개의 강의가 포함되어 있습니다.

“네이티브 모듈 사용”에서 뭘 배우나요?

성능이 중요한 작업을 위해 기존 Node.js 네이티브 모듈(C/C++)을 Electron 애플리케이션에 통합하고 사용하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Electron Desktop App Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Electron Desktop App Development을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Electron Desktop App Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“네이티브 모듈 사용” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Electron Desktop App Development 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Electron Desktop App Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 네이티브 모듈 사용
  2. 트레이 애플리케이션 및 배지
  3. 화면 캡처 및 미디어
  4. 전원과 하드웨어 모니터링
← Electron Desktop App Development(으)로 돌아가기