有没有办法编译 node.js 源文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6145561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to compile node.js source files?
采纳答案by Metal3d
I maybe very late but you can use "nexe" module that compile nodejs + your script in one executable: https://github.com/crcn/nexe
我可能很晚了,但您可以使用“nexe”模块在一个可执行文件中编译 nodejs + 您的脚本:https: //github.com/crcn/nexe
回答by Tor P
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?
Node.js 运行在 V8 Javascript 引擎之上,V8 Javascript 引擎本身通过将 javascript 代码编译为本机代码来优化性能……所以真的没有理由编译,是吗?
回答by TheFlash
附上JS。
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.
JavaScript 代码在编译时使用 V8 内部编译器转换为本机代码。因此,您的源代码不需要执行二进制文件,它们也没有打包。
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.
完美优化的本机代码只能在运行时基于客户端的机器生成。如果没有这些信息,EncloseJS 只能生成“未优化”的代码。它的运行速度比 NodeJS 慢大约 2 倍。
Also, node.js runtime code is put inside the executable (along with your code) to support node API for your application at run-time.
此外,node.js 运行时代码(连同您的代码)放在可执行文件中,以在运行时支持应用程序的节点 API。
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.
- 制作无源应用程序的商业版本。
- 制作无源应用的演示/评估/试用版。
- 制作某种自解压存档或安装程序。
- 使用 node-thrust 制作一个闭源 GUI 应用程序。
- 无需安装 node 和 npm 即可部署已编译的应用程序。
- 无需通过 npm install 下载数百个文件即可部署您的应用程序。将其部署为单个独立文件。
- 将您的资产放在可执行文件中,使其更加便携。在不安装的情况下针对新节点版本测试您的应用程序。
回答by Andre Jonker
There was an answer here: Secure distribution of NodeJS applications. Raynos said: V8 allows you to pre-compile JavaScript.
这里有一个答案:NodeJS 应用程序的安全分发。Raynos 说:V8 允许你预编译 JavaScript。
回答by Raynos
You can use the Closure compilerto compile your javascript.
您可以使用Closure 编译器来编译您的 javascript。
You can also use CoffeeScriptto compile your coffeescript to javascript.
您还可以使用CoffeeScript将您的 coffeescript 编译为 javascript。
What do you want to achieve with compiling?
你想通过编译达到什么目的?
The task of compiling arbitrary non-blocking JavaScript down to say, C sounds very daunting.
编译任意非阻塞 JavaScript 的任务可以说,C 听起来非常艰巨。
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.
编译为 C 或 ASM 确实没有那么快的速度。如果您想通过子进程将速度增益卸载计算到 C 程序。
回答by 1j01
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).
现在这可能包含的内容超出您的需要(甚至可能不适用于非图形环境中的命令行应用程序,我不知道),但是有nw.js。它是 Blink(即 Chromium/Webkit)+ io.js(即 Node.js)。
You can use node-webkit-builderto build native executable binaries for Linux, OS X and Windows.
您可以使用node-webkit-builder为 Linux、OS X 和 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-mainand not a main)
如果你想要一个 GUI,这是一个巨大的优势。您可以使用 Web 技术构建一个。如果您不这样做,请"node-main"在package.json( 并且可能"window": {"show": false}虽然它可能只具有 anode-main而不是 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 idealsolution for non-graphical Node.js applications.
我还没有尝试以这种方式使用它,只是将其作为一种可能性扔在那里。我可以说它肯定不是非图形 Node.js 应用程序的理想解决方案。
回答by Alfred
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 addonsand learn C. Although this is rather complex and I don't think you need to do this but instead just write javascript.
javascript 没有像 Java/C 这样的编译器(例如,您可以将它与 PHP 等语言进行比较)。如果你想编写编译代码,你应该阅读关于插件的部分并学习 C。虽然这相当复杂,我认为你不需要这样做,而只是编写 javascript。

