I'm trying to use pkg to bundle up a simple server app. It reads a sqlite3 file and serves it as JSON on port 3000, that's all. npm start works fine and keeps the app running.
When I pkg my server.js it creates the standalone executables as expected, but on both Windows and MacOS they exit immediately after starting. I have just enough time before the terminal window that it spawns closes to read that it is outputting the expected console logs for my app (the server's local IP address, some paths, etc.).
I've tried writing a script which forever-monitor to start server.js then pkging _that_ file instead, but the result is the same: it exits immediately after starting and the server doesn't continue running.
Is there any way to pkg an app so that it continues running indefinitely, or is there something I'm missing?
Turns out Terminal output _also_ included an error I did not have time to read:
$ /Users/jacob/repos/ib5k/cyberdefender/virus-slice-leaderboard-master/server-macos ; exit;
Your local network IP address is 192.168.1.108
Scoreboard data server running at http://192.168.1.108:3000
Scoreboard webpage live at http://192.168.1.108:3000/index.html
events.js:136
throw er; // Unhandled 'error' event
^
Error: SQLITE_CANTOPEN: unable to open database file
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
Still need to dig into why, but obviously this significantly changes my issue to my local code and sqlite3 config, rather than pkg.
But: the way I finally figured this out was MacOS Terminal > Preferences > [current profile] > Shell > When the shell exits: Don鈥檛 close the window. I had had Close if shell exited cleanly selected, meaning the window closes as soon as the error printed and process exited.
As for the other errors:
My app is supposed to read from a db file created by another application in a directory relative to the pkg'd script's location on the machine running it. pkg does funky things with paths involving a mysterious directory called /snapshot/, so I had to change my line like
path.resolve(__dirname, '../path/to/file/', 'Data.db')
to
path.resolve(process.execPath, '../../path/to/file/', 'Data.db')
then be careful where I put the pkg'd app executable.
To get sqlite3 to work, I had to copy the compiled node_sqlite3.node from sqlite3's node_modules file into the same directory as the pkg'd executable. To do this automatically, I added cp node_modules/sqlite3/lib/binding/**/node_sqlite3.node ../server to my script which runs pkg. The full deploy script in package.json reads:
"deploy": "npm run build && pkg . --out-path ../server && cp node_modules/sqlite3/lib/binding/**/node_sqlite3.node ../server"
Most helpful comment
As for the other errors:
One
My app is supposed to read from a
dbfile created by another application in a directory relative to thepkg'd script's location on the machine running it.pkgdoes funky things with paths involving a mysterious directory called/snapshot/, so I had to change my line liketo
then be careful where I put the
pkg'd app executable.Two
To get
sqlite3to work, I had to copy the compilednode_sqlite3.nodefromsqlite3'snode_modulesfile into the same directory as thepkg'd executable. To do this automatically, I addedcp node_modules/sqlite3/lib/binding/**/node_sqlite3.node ../serverto my script which runspkg. The fulldeployscript inpackage.jsonreads: