node-gyp c++ chrome extension

174 Views Asked by At

i need to create chrome extension that can call c++ from js so i use nodeapi and to achieve that I used the node-gyp

package.json

{
  "name": "test",
  "version": "0.13.0",
  "description": "test",
  "main": "./index.js",
  "files": [
    "index.js"
  ],
  "engines": {
    "node": ">=12.0.0"
  },
  "scripts": {
    "test": "node index.js",
    "build": "node-gyp rebuild",
    "clean": "node-gyp clean",
    "install": "prebuild-install --runtime napi || node-gyp rebuild"
  },
  "dependencies": {
    "bindings": "^1.5.0",
    "browserify": "^17.0.0"
    "node-addon-api": "^3.0.2",
    "prebuild-install": "^6.0.0"
  },
  "devDependencies": {
    "babel-eslint": "^10.1.0",
    "chai": "^4.1.2",
    "chai-spies": "^1.0.0",
    "eslint": "^6.8.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-json": "^1.2.0",
    "eslint-plugin-mocha": "^6.2.2",
    "mocha": "^5.0.4",
    "node-gyp": "^9.1.0",
    "prebuild": "^10.0.1",
    "rimraf": "^2.6.2"
  },
  "gypfile": true
}

binding.gyp

{
  "targets": [
    {
        'target_name': 'hello',
        'sources': [ 'hello.cc' ],
        'defines': [
            '_LARGEFILE_SOURCE',
            '_FILE_OFFSET_BITS=64',
            'NAPI_DISABLE_CPP_EXCEPTIONS'
        ],
        'cflags!': ['-ansi', '-fno-exceptions' ],
        'cflags_cc!': [ '-fno-exceptions' ],
        'cflags': ['-g', '-exceptions'],
        'cflags_cc': ['-g', '-exceptions'],
        'include_dirs': [
          "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'dependencies': [
          "<!(node -p \"require('node-addon-api').gyp\")"
        ],
    }, 
  ]
}

hello.cc

#include <napi.h>
Napi::String Method(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();
  return Napi::String::New(env, "Hello Calling From Cpp");
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "hellocc"), Napi::Function::New(env, Method));
  return exports;
}

NODE_API_MODULE(hello, Init)

index.js

 'use strict'
 var addon = require('bindings')('hello');
 function testingfromjs(){
     return addon.hellocc();
 }
exports.testingfromjs= testingfromjs;

the problem is it works fine with commands like node index.js but it didn't work when it build as chrome extension i imported it as dependency and use this as required in the main.js of chrome extension but it didn't work

0

There are 0 best solutions below