Node.js plug-in writing (1) - Getting started with writing simple plug-ins through Node-Api

Before writing Node plug-ins, we must first understand how to write node.js plug-ins. Today we use Node-Api, one of the three ways to write, and use C++ to implement. You can also write your own plug-ins according to the following steps.

But writing a plug-in requires a C/C++ foundation. If you don't have a foundation, it is recommended to ignore this article.

Environmental preparation

Python 3.x environment 

Node.js  15.x+

gcc 8.2.x     /VC2022  

MacOS /Windows

Write code

Create a plugin folder and create a package.json file with the following contents

{
  "name": "hello_world",
  "version": "0.0.0",
  "description": "Node.js Addons Example #1",
  "main": "hello.js",
  "private": true,
  "dependencies": {
    "bindings": "~1.2.1",
    "node-addon-api": "^1.0.0"
  },
  "scripts": {
    "test": "node hello.js"
  },
  "gypfile": true
}

Create the hello.cc plug-in source code file in the same directory, the content is as follows

#include <napi.h>

Napi::String Method(const Napi::CallbackI

Guess you like

Origin blog.csdn.net/yue7603835/article/details/122126141