Search code examples
node.jswindowsbashcmdmacos-sierra

Creating self-executable node.js server


I would like to create an executable bash file that will execute the command node app.js - the goal is to have an executable file that can be opened on a local machine on which node is already installed and the executable is inside the project directory (It's an express.js app).

I'm trying to do it on a mac, I've written a .command file, made it executable and added the command. Problem is, the process immediately closes, as the terminal seems to automatically add exit; after the last command. I would like to do it on Windows machines also.

I don't need the entire app in an executable file, only the option to double click a file instead of opening the terminal and manually writing that simple command.

Any idea how to achieve this?


Solution

  • I'm afraid nothing of suggested solutions worked or was good enough to use - but I've managed to solve this using:

    reldir=`dirname $0`
    cd $reldir
    npm start
    

    @Gregory was the closest to the solution -

    First - find the current directory path and go to it. next - differently from the command node app.js, calling npm start seems to execute after process exits, so it just kept alive.