求助:基于node-gyp的Chrome扩展调用C++模块失败
Chrome扩展中Node-API调用C++失效问题
我要开发一个能从JavaScript调用C++的Chrome扩展,采用Node-API配合node-gyp实现。Node环境下测试正常,但将其作为依赖引入Chrome扩展的main.js后无法正常工作。以下是相关代码文件:
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;
使用node index.js等Node环境命令测试时一切正常,但将该模块作为依赖引入Chrome扩展的main.js后,无法正常工作。
内容的提问来源于stack exchange,提问作者Ahmed Shams
相关产品推荐
相关产品推荐

