Santosh Hegde Santosh Hegde - 4 months ago 24
Node.js Question

How to run node project in a linux VM?

I have a express node project. Im 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 stoping.

So how to 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"
}
}

Answer

You can use forever,

npm install -g forever

and can start with:

forever start yourfile.js

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" yourfile.js dotenv_config_path=.development