I maybe very late but you can use "nexe" module that compile nodejs + your script in one executable: https://github.com/crcn/nexe
EDIT 2021: Nexe's latest release is from 2017 and it appears that development has otherwise slowed, so the more-widely-used alternative from Vercel should also be considered these days: pkg
Node.js runs on top of the V8 Javascript engine, which itself optimizes performance by compiling javascript code into native code... so no reason really for compiling then, is there?
JavaScript code is transformed into native code at compile-time using V8 internal compiler. Hence, your sources are not required to execute the binary, and they are not packaged.
Perfectly optimized native code can be generated only at run-time based on the client's machine. Without that info EncloseJS can generate only "unoptimized" code. It runs about 2x slower than NodeJS.
Also, node.js runtime code is put inside the executable (along with your code) to support node API for your application at run-time.
Use cases:
Make a commercial version of your application without sources.
Make a demo/evaluation/trial version of your app without sources.
Make some kind of self-extracting archive or installer.
Make a closed source GUI application using node-thrust.
No need to install node and npm to deploy the compiled application.
No need to download hundreds of files via npm install to deploy your application. Deploy it as a single independent file.
Put your assets inside the executable to make it even more portable. Test your app against new node version without installing it.
You can also use CoffeeScript to compile your coffeescript to javascript.
What do you want to achieve with compiling?
The task of compiling arbitrary non-blocking JavaScript down to say, C sounds very daunting.
There really isn't that much speed to be gained by compiling to C or ASM. If you want speed gain offload computation to a C program through a sub process.
Now this may include more than you need (and may not even work for command line applications in a non-graphical environment, I don't know), but there is nw.js. It's Blink (i.e. Chromium/Webkit) + io.js (i.e. Node.js).
You can use node-webkit-builder to build native executable binaries for Linux, OS X and Windows.
If you want a GUI, that's a huge plus. You can build one with web technologies. If you don't, specify "node-main" in the package.json (and probably "window": {"show": false} although maybe it works to just have a node-main and not a main)
I haven't tried to use it in exactly this way, just throwing it out there as a possibility. I can say it's certainly not an ideal solution for non-graphical Node.js applications.
javascript does not not have a compiler like for example Java/C(You can compare it more to languages like PHP for example). If you want to write compiled code you should read the section about addons and learn C. Although this is rather complex and I don't think you need to do this but instead just write javascript.
发布评论
评论(7)
我可能很晚了,但你可以使用“nexe”模块在一个可执行文件中编译nodejs +你的脚本: https://github .com/crcn/nexe
编辑 2021: Nexe 的最新版本是从 2017 年开始的,而且开发速度似乎已经放缓,因此也应该考虑使用 Vercel 的更广泛使用的替代方案天: pkg
I maybe very late but you can use "nexe" module that compile nodejs + your script in one executable: https://github.com/crcn/nexe
EDIT 2021: Nexe's latest release is from 2017 and it appears that development has otherwise slowed, so the more-widely-used alternative from Vercel should also be considered these days: pkg
Node.js 运行在 V8 Javascript 引擎之上,该引擎本身通过将 javascript 代码编译为本机代码来优化性能......所以没有理由真正编译,是吗?
https://developers.google.com/v8/design#mach_code
Node.js runs on top of the V8 Javascript engine, which itself optimizes performance by compiling javascript code into native code... so no reason really for compiling then, is there?
https://developers.google.com/v8/design#mach_code
EncloseJS。
您将获得一个功能齐全的二进制文件,无需源代码。
还支持原生模块。(必须放在同一文件夹中)
JavaScript代码被转换为使用 V8 内部编译器在编译时生成本机代码。因此,您的源代码不需要执行二进制文件,并且它们不会被打包。
完美优化的本机代码只能在运行时基于客户端的机器生成。如果没有这些信息,EncloseJS 只能生成“未优化”的代码。它的运行速度比 NodeJS 慢大约 2 倍。
此外,node.js 运行时代码放入可执行文件(与您的代码一起),以在运行时支持应用程序的 Node API。
使用案例:
针对新的节点版本测试您的应用程序,而无需安装它。
EncloseJS.
You get a fully functional binary without sources.
Native modules also supported. (must be placed in the same folder)
JavaScript code is transformed into native code at compile-time using V8 internal compiler. Hence, your sources are not required to execute the binary, and they are not packaged.
Perfectly optimized native code can be generated only at run-time based on the client's machine. Without that info EncloseJS can generate only "unoptimized" code. It runs about 2x slower than NodeJS.
Also, node.js runtime code is put inside the executable (along with your code) to support node API for your application at run-time.
Use cases:
Test your app against new node version without installing it.
这里有一个答案:NodeJS 应用程序的安全分发。 Raynos 说:V8 允许你预编译 JavaScript。
There was an answer here: Secure distribution of NodeJS applications. Raynos said: V8 allows you to pre-compile JavaScript.
您可以使用Closure 编译器来编译您的 JavaScript。
您还可以使用 CoffeeScript 将您的 CoffeeScript 编译为 JavaScript。
你想通过编译达到什么目的?
编译任意非阻塞 JavaScript 的任务对于 C 来说听起来非常艰巨。
编译为 C 或 ASM 确实无法获得那么快的速度。如果您希望通过子进程将速度增益卸载到 C 程序。
You can use the Closure compiler to compile your javascript.
You can also use CoffeeScript to compile your coffeescript to javascript.
What do you want to achieve with compiling?
The task of compiling arbitrary non-blocking JavaScript down to say, C sounds very daunting.
There really isn't that much speed to be gained by compiling to C or ASM. If you want speed gain offload computation to a C program through a sub process.
现在,这可能包括超出您需要的内容(甚至可能不适用于非图形环境中的命令行应用程序,我不知道),但是有 nw.js。
它是 Blink(即 Chromium/Webkit)+ io.js(即 Node.js)。
您可以使用 node-webkit-builder 为 Linux、OS X 和视窗。
如果你想要一个 GUI,那是一个巨大的优势。您可以使用网络技术构建一个。
如果不这样做,请在
package.json
中指定"node-main"
(可能还指定"window": {"show": false} 虽然也许只有一个
node-main
而不是main
才有效)我还没有尝试以这种方式使用它,只是把它扔在那里作为一种可能性。我可以说,对于非图形 Node.js 应用程序来说,这当然不是一个理想解决方案。
Now this may include more than you need (and may not even work for command line applications in a non-graphical environment, I don't know), but there is nw.js.
It's Blink (i.e. Chromium/Webkit) + io.js (i.e. Node.js).
You can use node-webkit-builder to build native executable binaries for Linux, OS X and Windows.
If you want a GUI, that's a huge plus. You can build one with web technologies.
If you don't, specify
"node-main"
in thepackage.json
(and probably"window": {"show": false}
although maybe it works to just have anode-main
and not amain
)I haven't tried to use it in exactly this way, just throwing it out there as a possibility. I can say it's certainly not an ideal solution for non-graphical Node.js applications.
javascript 没有像 Java/C 这样的编译器(您可以将其与 PHP 等语言进行更多比较)。如果您想编写编译代码,您应该阅读有关 插件 的部分并学习 C。虽然这相当复杂,而且我认为你不需要这样做,而只需编写 javascript 即可。
javascript does not not have a compiler like for example Java/C(You can compare it more to languages like PHP for example). If you want to write compiled code you should read the section about addons and learn C. Although this is rather complex and I don't think you need to do this but instead just write javascript.