1
votes

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?

1
@Subburaj MSI is an installer package file format used by Windows, what makes you think it's needed for macOS?Patrick Hund
@Fakhreddin, I think you are on the right track with pkg, like you say, it does support creating executables for both Windows and Mac. What exactly does "I was unable to use the macOS version" mean? What problems did you encounter? Error messages, etc.?Patrick Hund
@PatrickHund Thanks for your attention, Patrick. What I meant was that I need to run my application as an executable. As you may know, in order to run a node js application you need to run it from the command prompt i.g. "node index.js". Instead, I need to run my application by double-clicking on it. To do so, I created .exe file for Windows but I couldn't do the same for Mac. I do not know how to work with the wierd file format that "node pkg" generates for Mac.Fakhreddin Amirhosseini
@PatrickHund In addition, I run my .exe file as a Windows service so that my application runs on the background. Is there a simular solution for Mac?Fakhreddin Amirhosseini

1 Answers

0
votes

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