Getting tiddlywiki to Run in the Background using PM2

This is a very simple post, but one that I could find almost no resources about! It could be that no one really uses tiddlywiki (I love personal wikis!) or it could be that everyone knows how to use variables in PM2 out of the box, but because I didn’t, this took much longer than necessary to figure out.
I’ll cut right to the chase — I use tiddlywiki as a personal wiki at work to keep all my long-term notes in a searchable, editable, easy-to-access location. If you haven’t tried it I would definitely give it a try. There are several variations of personal wikis, but the beauty of tiddlywiki is it is database free. Instead, it uses one file per article, and those files are written in regular markdown which can then be taken and pasted right into a readme, github wiki, etc.
Anyway, I installed it using the npm setup, very easy, but wanted it to run on a custom port (4040). Easy enough, but then the trouble began. I didn’t want to have a terminal open every time I edited the wiki. I knew PM2 should be able to handle that, but for some reason could not get it to run on port 4040. And then, in a moment of victory this morning, I figured it out!
(This function goes in your ~/.bash_profile. It can be accessed by opening a terminal and typing wiki.)
function wiki {
pm2 delete tiddlywiki # in case it was already running
cd ~/personal-wiki # path to where files are stored
pm2 start --name tiddlywiki ~/.npm-global/lib/node_modules/tiddlywiki/tiddlywiki.js -- --server 4040
echo "Starting Wiki... (Browser will open automatically)"
sleep .9
open http://localhost:4040
}The line that took me forever to figure out? pm2 start … /path/to/executable.js -- —-server 4040. Why the extra -- in there? It designates the end of PM2 variables, and the beginning of script variables. Very confusing in my opinion, but so it is!
So if you have been searching the internet for a way to get your tiddlywiki to stop filling up your terminal windows? You’re welcome.
