2

I have an express node project. I am starting the server by running command npm start inside project folder in my local and it works as expected.

I tried to run the same command on remote linux VM. But after closing the terminal, server is shutting down. So I tried to start with npm start & and nohup npm start &. But still after closing the terminal server is stopping.

So how can I start the node server for express project in background?

My package.json files looks like this:

{
  "name": "test_project",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "concurrently \"tsc -w\" \"node ./bin/www\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings install",
    "bundle": "node bin/bundler.js",
    "bundle:prod": "node bin/bundler.js --prod"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0",
    "mongodb": "2.2.4",
    "request": "~2.73.0"
  },
  "devDependencies": {
    "@angular/common": "2.0.0-rc.3",
    "@angular/compiler": "2.0.0-rc.3",
    "@angular/core": "2.0.0-rc.3",
    "@angular/http": "2.0.0-rc.3",
    "@angular/platform-browser": "2.0.0-rc.3",
    "@angular/platform-browser-dynamic": "2.0.0-rc.3",
    "@angular/router": "^3.0.0-alpha.7",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.3",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.12",
    "concurrently": "^2.0.0",
    "systemjs-builder": "^0.15.17",
    "typescript": "^1.8.10",
    "typings": "^1.3.0",
    "yargs": "^4.7.1"
  }
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51
  • what if you say `npm start` and then `disown`? From [How can I close a terminal without killing the command running in it?](http://unix.stackexchange.com/q/4004/40596) – fedorqui Jul 25 '16 at 13:08
  • After 2 mins it stopped. **Hangup** present in nohup.out – Santosh Hegde Jul 25 '16 at 13:13

4 Answers4

1

You can use forever,

npm install -g forever

and can start with:

forever start bin/www

Maybe this is not the best way for your situation but it should work. And you can see the working proccess with forever like this:

forever list

Remember that, you should start forever when you're in the project directory.

And I recommend dotenv. https://www.npmjs.com/package/dotenv you can set your environment according to your work space. And if you use forever and dotenv together, you can use like this:

forever start -c "node -r dotenv/config" bin/www dotenv_config_path=.development
  • Okay, you dont have to put forever into your package.json just install **forever** as global as i said. And i think you use express framework. you can work like `forever start bin/www`. Forever is good, it can keep working even your node.js application crashed! You should try :) –  Jul 25 '16 at 13:32
0

You can use the npm package "forever":

install it as global:

[sudo] npm install forever -g

After run:

forever start app.js

Full documentation: https://www.npmjs.com/package/forever

Umberto
  • 76
  • 5
0

Instead of starting it in a terminal you can Run it as a service on many platforms. The answer in the post above gives a great example. Also, it doesn't require any additional packages.

Community
  • 1
  • 1
leetibbett
  • 843
  • 4
  • 16
0

Another best tool to run node app is pm2

Install and run using pm2

npm install pm2 -g
pm2 start app.js

Read more about pm2 at http://pm2.keymetrics.io/

Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51