简体   繁体   English

macOS 的 node js 应用程序捆绑

[英]node js application bundling for macOS

I have developed a node js application.我开发了一个节点js应用程序。 I have managed to bundle my application to a .exe file using node pkg which runs perfectly on windows.我已经设法使用在 Windows 上完美运行的node pkg将我的应用程序捆绑到一个 .exe 文件中。 Now I need to do the same thing for macOS so that my application can be run on Apple computers.现在我需要为 macOS 做同样的事情,以便我的应用程序可以在 Apple 计算机上运行。 The node pkg itself also provides executables for Linux and macOS but I was unable to use the macOS version. node pkg本身也为 Linux 和 macOS 提供了可执行文件,但我无法使用 macOS 版本。 Are there any ways to develop software for macOS with node js?有没有什么方法可以用 node js 为 macOS 开发软件? If there is what are the file formats for macOS which work similarly to a .exe file in Windows?如果 macOS 有哪些文件格式与 Windows 中的 .exe 文件类似?

When you package your Node.js application with pkg , by default, executables for Windows, Linux and macOS are created.使用pkg打包 Node.js 应用程序时,默认情况下会创建适用于 Windows、Linux 和 macOS 的可执行文件。

This minimal example creates an executable that prints “HELLO WORLD” to the console:这个最小的例子创建了一个可执行文件,将“HELLO WORLD”打印到控制台:

$ echo "console.log('HELLO WORLD')" > index.js
$ npx pkg index.js
npx: installed 138 in 5.155s
> pkg@4.4.2
> Targets not specified. Assuming:
  node10-linux-x64, node10-macos-x64, node10-win-x64

This produces the following executables:这会产生以下可执行文件:

$ ls -l
total 227360
-rwxr-xr-x  1 pahund  110015913  39698697 Dec 24 14:03 index-linux
-rwxr-xr-x  1 pahund  110015913  40712141 Dec 24 14:03 index-macos
-rw-r--r--  1 pahund  110015913  35979810 Dec 24 14:03 index-win.exe

In a macOS terminal, you can tell by the file access setting rwxr-xr-x that index-linux and index-macos are executable (that's what the “x” means).在 macOS 终端中,您可以通过文件访问设置rwxr-xr-x判断index-linuxindex-macos是可执行的(这就是“x”的意思)。

The file ending .exe is specific to windows and not used in Linux and macOS..exe结尾的文件特定于 Windows,在 Linux 和 macOS 中不使用。 That does not mean that index-linux and index-macos are not executable.这并不意味着index-linuxindex-macos不可执行。

You can run them from the terminal like any other binary or script:您可以像任何其他二进制文件或脚本一样从终端运行它们:

$ ./index-macos
HELLO WORLD

(notice that the path ./ is necessary when running in a terminal) (请注意,在终端中运行时,路径./是必需的)

You can also locate index-macos in the macOS Finder and double-click it, this will open a terminal window that prints “HELLO WORLD”.您也可以在 macOS Finder 中找到index-macos并双击它,这将打开一个打印“HELLO WORLD”的终端窗口。

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

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