2

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?

1
  • Try the command nohup node app.js & instead. But it'll make a nohup file with the console output written in it for debug purpose. I don't know how to do without creating that file ^^ (maybe a nohup flag) Commented Mar 31, 2017 at 0:28

5 Answers 5

2

forever should do this.

In additional to node, install forever:

npm i -g forever

And change the command to:

forever app.js

The server should run continuously after the process closes.

To stop the server, you can use another .command file with this command:

forever stopall

Sign up to request clarification or add additional context in comments.

Comments

1

hotel might be a good general solution for your use case.

Once registered with the command hotel add, you can start and stop your server from a simple web interface.

Its a better cross-platform solution too, since otherwise using executables means you'd need:

  • .bat for Windows
  • .sh for Linux
  • .command for Mac

My favorite feature though is the proxy support. For example, once I registered my node project my-express-app as such:

$ cd my-express-app
$ hotel add "node app.js"

I can simply open up my browser and nagivate to http://my-express-app.dev to start the server.

Comments

1
+50

.command run in the User folder, so you have to enter the project folder to run the node script

cd /something/project
node app.js 

it worked for me on mac.

windows i think you just need to make it a .bat and it should work

you could use npm start instead of node app.js

Comments

0

Write below command in a file

node app.js

and save it with .bat extension.

Note: first try to execute your app.js normally because if it gets any error then your shell command will stop and will close the terminal.

OR

create a .bat file with the name runCommand.bat, inside it write:

node app.js
cmd /K runcommand.bat

save it and execute with the double click. If it finds error or server crashed it will keep retrying until you didn't fix it. else will keep running.

To achieve the required result you can use FOREVER or PM2 to manage the process.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.