Node.js interpreter as a C/C++ library

961 views
Skip to first unread message

Daniel Zduniak

unread,
Feb 15, 2015, 1:17:38 PM2/15/15
to nod...@googlegroups.com
I'm interested in developing a solution that would allow to create desktop apps with Node.js.

I believe requiring user to install whole Node.js platform would seem to be too cumbersome and just unnecessary since the user wouldn't really need full developer environment for his needs - he only needs to be able to run the app. It would be especially undesired on mobile, where installing Node.js is simply difficult or maybe even impossible...

So the solution I'm proposing would be to use special launcher with embedded Node.js interpreter that would then run proper Node.js app...

Is something like this currently possible? If yes could you provide me with some hints?

Aria Stewart

unread,
Feb 15, 2015, 2:43:45 PM2/15/15
to nod...@googlegroups.com
Desktop apps with node are already created using NW.js and atom-shell and other tools -- the Atom text editor is already successful doing this.

Making a shared library version of the interpreter is possible, or at least was recently, but may not go as far as you think.

A lot of this has been explored, to varying degrees in a lot of projects.

Eric Muyser

unread,
Feb 15, 2015, 2:44:15 PM2/15/15
to nod...@googlegroups.com
There must be a few solutions out there by now. Even in 2012, there was Titanium Appcelerator.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/704b0e91-5d5b-4155-8b1c-09f2ded348a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Ingwersen (Ingwie Phoenix)

unread,
Feb 15, 2015, 2:44:16 PM2/15/15
to nod...@googlegroups.com
NodeJS is based on the v8 JavaScript engine - thus, it is ment to be a standalone app…so embedding it is not a goal you can reach. NodeJS’ core logic relies on being the „one and only“ part of the app - initializing an uv event loop and v8 instance for the process. There is no such way of extending an existing v8 instance with the NodeJS environment (too bad! It’s a shame, really…).

But, there are alternatives:

- Desktop can have nw.js: http://nwjs.io/
- Mobile could use https://github.com/paddybyers/anode

There is currently no library or such that maps all NodeJS features into a library, that you can simply use. The main problem I see is that NodeJS requires a uv loop which blocks the currently running thread. To sum up what code NodeJS does

using v8;
// Make the JS context
Isolate* isolate = new Isolate();
context* ctx = new Context(isolate);
// Add NodeJS APIs
node::prepare_environment(ctx);
// Start, run and when no events are left, end the loop.
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
uv_loop_init(loop);
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
free(loop);

The fact that the current thread is blocked may be bad for most applications. Therefore, I can suggest you to actually look into WebViews. These are usually UI classes - different on each platform though… - that allow you to open a website, and in most case, modify the JavaScript environment. I’d suggest you to add common APIs yourself.

Patrick Debois

unread,
Feb 15, 2015, 2:44:38 PM2/15/15
to nod...@googlegroups.com
Have a look at this thread - https://github.com/crcn/nexe/issues/15


--

Peter Rust

unread,
Feb 16, 2015, 2:09:19 PM2/16/15
to nod...@googlegroups.com
Daniel,

> requiring user to install whole Node.js platform would seem to be too cumbersome

Node is just a single standalone executable, you can drop it into your project and use it without it being cumbersome. The installer just puts the executable in your path and also installs npm (which isn't necessary for end-users of a desktop app that depends on node).

It depends on your use-case, but in general I think you'll find it easier to run node.exe as a child process and to communicate with it via stdio, than to embed it or compile it as part of your main application.

If you're interested in building a Windows GUI (not a console app) then you'll run into issues b/c Node is setup as a console app. It should be possible, but it would require changing the subsystem and providing some dummy stdin/stdout (more info here, here and here).

As others have mentioned, nw.js (formerly node-webkit) and atom-shell are the highest-profile platforms that do what you're looking for. nw.js actually merges the node.js and chromium event loops into one, so the project has to maintain a hacked version of node & a hacked version of Chromium. atom-shell, OTOH, follows the separate process approach outlined above. I've done something similar, creating a custom build of Chromium and communicating with the node executable via Chromium's native messaging.

> It would be especially undesired on mobile

Mobile is a different ballgame, especially if you want to put your app in iOS's App Store. If you're content with running your app on jailbroken phones, you can do it with NodObjC, but it won't run on an iPhone that isn't jailbroken b/c Node is tightly tied to v8 which does JIT compilation which isn't allowed on iPhones for security reasons. Instead you'll need to use a project that uses pieces of Node's implementation or just mimics Node's API with a non-JIT javascript engine like JavaScriptCore, such as Node.app/Nodelike or neu.Node.

If you're not married to Javascript, you might consider a C/C++ library that provide a node-like API (node.native, nope.c, node.c, or a combo of tiny clibs) with skia or cairo for cross-platform graphics/GUI.

-- Peter Rust

Peter Rust

unread,
Feb 16, 2015, 2:12:10 PM2/16/15
to nod...@googlegroups.com
Daniel,

I missed a C/C++ implementation of Node's API:


-- peter
Reply all
Reply to author
Forward
0 new messages