简体   繁体   English

是否可以为浏览器捆绑 Node 插件?

[英]Is it possible to bundle Node addons for browsers?

I have a very simple C++ node addon.我有一个非常简单的 C++ 节点插件。 It exports 2 methods using the NODE_SET_METHOD node function.它使用NODE_SET_METHOD节点 function 导出 2 种方法。 Both of them simply return a string and/or print something to the console.他们都只是简单地返回一个字符串和/或打印一些东西到控制台。

void Method1(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    args.GetReturnValue().Set(String::NewFromUtf8(
        isolate, "world"));
}

void Method2(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    printf("Testing testing.\n");
    args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello there"));
}

void Initialize(Local<Object> exports) {
    NODE_SET_METHOD(exports, "hello", Method1);
    NODE_SET_METHOD(exports, "welcome", Method2);
}

The build is working just fine.构建工作正常。 Calling those functions from JavaScript through Node also returns and prints out the expected values.通过 Node 从 JavaScript 调用这些函数也会返回并打印出预期值。 What I am struggling with is to bundle this entire thing (using browserify) so that I can call the same functions from the browser.我正在努力的是捆绑整个事情(使用browserify),以便我可以从浏览器调用相同的功能。 Browserify refuses to build. Browserify 拒绝构建。 I keep getting this error message:我不断收到此错误消息:

> npx browserify index.js
Error: Can't walk dependency graph: Cannot find module './build/Release/hello' from 'F:\workspace\c\addon\hello\index.js'
    required by F:\workspace\c\addon\hello\index.js
    at F:\workspace\c\addon\hello\node_modules\resolve\lib\async.js:136:35
    at load (F:\workspace\c\addon\hello\node_modules\resolve\lib\async.js:155:43)
    at onex (F:\workspace\c\addon\hello\node_modules\resolve\lib\async.js:180:17)
    at F:\workspace\c\addon\hello\node_modules\resolve\lib\async.js:15:69
    at FSReqCallback.oncomplete (fs.js:166:21)

I am starting to think that it is not possible to bundle node addons for front end use.我开始认为不可能捆绑节点插件以供前端使用。 Is there a way, however, to get around this problem and be able to include node addons for browser builds?但是,有没有办法解决这个问题并能够为浏览器构建包含节点插件? Maybe a special way to write my index.js or something?也许是一种特殊的方式来编写我的index.js的? Or is there a different way to create addons which circumvents this problem?或者有没有不同的方法来创建绕过这个问题的插件?

The following repository has a good set of node native add-on examples以下存储库有一组很好的节点本机附加示例

node-addon-examples节点插件示例

Node add-on cannot bundle with browsers ;节点插件不能与浏览器捆绑 the return value scenario is explained in返回值场景在

hello_world example hello_world 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM