简体   繁体   中英

node js application bundling for macOS

I have developed a node js application. I have managed to bundle my application to a .exe file using node pkg which runs perfectly on windows. Now I need to do the same thing for macOS so that my application can be run on Apple computers. The node pkg itself also provides executables for Linux and macOS but I was unable to use the macOS version. Are there any ways to develop software for macOS with node js? If there is what are the file formats for macOS which work similarly to a .exe file in Windows?

When you package your Node.js application with pkg , by default, executables for Windows, Linux and macOS are created.

This minimal example creates an executable that prints “HELLO WORLD” to the console:

$ 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).

The file ending .exe is specific to windows and not used in Linux and macOS. That does not mean that index-linux and index-macos are not executable.

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”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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