Reputation: 65
Is it possible to load native add-ons (written in C/C++ like in nodejs) from client side javascript using requireJS or some other module?
I am writing a nodejs + express app that serves an html file which loads javascript files. I am using requireJS and compiling these client-side js files into AMD module. I can load and call native methods from the nodejs server side js scripts and wondering if its possible to do the same from the client side js scripts.
Upvotes: 1
Views: 698
Reputation: 151390
RequireJS is not the driving factor in what you are trying to achieve. The capability to load native code is solely dependent on whether the JavaScript VM that runs your code allows it or not. If the VM does not allow it, RequireJS cannot add this capability.
You can do it in Node because Node allows it.
Browsers are much more restrictive as to what they allow. A JavaScript script executing in a web page won't generally be able to load native code because, as Chris Franklin explained, it is a security hole.
One way to get the functionality provided by a C or C++ library to run browser-side is to use something like Emscripten to compile the C or C++ code to JavaScript.
Upvotes: 0
Reputation: 3984
No, you can't force the client to execute a native application via the browser like that. It would present a very large security hole that would allow any hacker to come along and execute arbitrary violent code on any client and this is strictly prevented. You can execute the C++ add-on on the server side via an API though, and that should solve most use cases.
Upvotes: 1