Writing node.js plugins using node-addon-api

Go directly to the dry goods.

step:

Step 1: Write the binding.gyp file, refer to the node-gyp document  node-gyp - npm

{
  "targets": [
    {
      "target_name": "ShareMemory",
      "cflags!": [ "-fno-exceptions" ],
      "cflags_cc!": [ "-fno-exceptions" ],
      "sources": [ "./mmap.cc" ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

Step 2: Write C++ code, refer to node-addon-api documentation  node-addon-api - npm

Step 3: Install node-gyp, refer to node-gyp documentation

npm install -g node-gyp

Step 4: Install Python3

Step 5: Build project files, refer to node-gyp documentation 

node-gyp configure --python "C:\Python310\python.exe"

When building, you can add build parameters. For example, if I need to compile a 32-bit version of Windows, I can add the parameter --arch=ia32. For other parameters, please refer to the node-gyp documentation.

node-gyp configure --python "C:\Python310\python.exe" --arch=ia32

Step 6: Compile the project and produce .node files

node-gyp build --python "C:\Python310\python.exe"

Without further ado, done!

Guess you like

Origin blog.csdn.net/ctbinzi/article/details/121957843