How do I run a node.js app as a background service

2018-12-31 10:02发布

Since this post has gotten a lot of attention over the years, I've listed the top solutions per platform at the bottom of this post.


Original post:

I want my node.js server to run in the background, i.e.: when I close my terminal I want my server to keep running. I've googled this and came up with this tutorial, however it doesn't work as intended. So instead of using that daemon script, I thought I just used the output redirection (the 2>&1 >> file part), but this too does not exit - I get a blank line in my terminal, like it's waiting for output/errors.

I've also tried to put the process in the background, but as soon as I close my terminal the process is killed as well.

So how can I leave it running when I shut down my local computer?


Top solutions:

25条回答
无与为乐者.
2楼-- · 2018-12-31 10:40

Its very simple.

  1. Add package.json in your project
  2. Add script file name or path in your Package.JSON Start
  3. Then simple go to your console open your project directory by cd path/to/directory/
  4. Write nohup npm start

Following is a Package.JSON sample that anyone can use. { "name": "Project",

  "version": "1.0.0",

  "main": "httpsserver.js",

  "scripts": {

    "start": "node httpsserver.js"
  },

  "keywords": [],

  "author": "",

  "license": "ISC",

  "dependencies": {},

  "devDependencies": {},

  "description": ""

}
查看更多
登录 后发表回答