Skip to content

Guide · Open source

How to use forever: run a Node.js script continuously

Updated · written with AI from the project's own documentation and checked against our catalogue data

forever is a command-line tool that runs a script and restarts it if it stops or crashes. It is aimed at developers who want to keep a small Node.js script running on a server, and it can also run that script in the background as a daemon. This guide shows how to install it, start and stop scripts, use common options and JSON configuration files, and decide whether it is the right choice for a new project.

foreverforeversd/forever · ★ 13.8k · MIT

Quick start

  1. 1Install Node.js and npm. Make sure Node.js and npm are installed on the machine where your script will run.
  2. 2Install forever globally. Run the global npm install command so the forever command is available.
  3. 3Start your script. Run forever start with the path to your script, such as app.js.
  4. 4Add log files for background runs. When running as a daemon, add -l, -o and -e so you can read the forever, stdout and stderr logs.
  5. 5Check and manage processes. Use forever list to see running scripts and forever restart or forever stop to manage them.
  6. 6Optionally save settings in a JSON file. Move your options into a JSON file and start it with forever start followed by the file path.

Install forever with npm

forever is installed with npm, so you need Node.js and npm on your machine first. The README installs it globally so the forever command is available from any directory.

  • Prerequisites: Node.js and npm
$ [sudo] npm install forever -g

Start a script with forever

Pass forever the path to your script and it will run the script and restart it if it stops. forever can run scripts written in Node.js or in other languages, since the README says it works whether or not the script is Node.js.

To limit how many times a script is restarted during testing, use the -m option. The README gives this example, which runs a test script that errors on a timer and stops after 5 runs.

forever start app.js $ forever -m 5 examples/error-on-timer.js

Run a script as a background daemon

The daemon mode starts the target process in the background. The README describes this as useful for starting simple Node.js scripts remotely without using nohup. It recommends using the -l, -o and -e options so you have log files for the forever process, stdout and stderr.

To stop a daemon, use forever stop with the script name.

  • -l LOGFILE logs the forever output
  • -o OUTFILE logs stdout from the child script
  • -e ERRFILE logs stderr from the child script
forever start -l forever.log -o out.log -e err.log my-daemon.js forever stop my-daemon.js

Manage running scripts

forever has commands to list, restart and stop the scripts it manages, and to read their logs. The list command shows running scripts, and you can choose which columns it displays.

Stop everything at once with stopall, or restart everything with restartall. The logs command lists log files, and logs followed by a script name or index tails that script's logs.

  • forever list: list all running forever scripts
  • forever restart: restart the daemon script
  • forever stopall: stop all running forever scripts
  • forever logs <script|index>: tail the logs for a script
  • forever cleanlogs: deletes all historical forever log files (the README marks this as careful)
forever list forever restart my-daemon.js forever stopall

Use a JSON configuration file

Instead of typing options on the command line, you can put them in a JSON file and pass that file to forever. The README notes that forever parses these files with shush, so in-line comments are allowed. A file can also define several applications at once as a JSON array, each with its own options such as uid and sourceDir.

  • Relative and absolute paths to the JSON file both work
  • Options include uid, append, watch, script, sourceDir, logFile, outFile and errFile
$ forever start ./forever/development.json

Use forever from your own code

forever exposes methods for use in code, and each returns an EventEmitter. Since forever 0.6.0, processes are not automatically listed in forever.list(). To get them listed, you must start the forever socket server with forever.startServer.

The README says that if you use forever programmatically you should install forever-monitor instead, so follow that route for code-level control.

forever.startServer(child);

What people use it for

  • Keeping a small Node.js script running on a remote server without writing a service file
  • Running a script in the background without using nohup, as the README describes
  • Limiting restarts during testing with -m to see how a crashing script behaves
  • Running several apps from one JSON file, each with its own source directory and options
  • Maintaining an existing project that already uses forever

Frequently asked questions

Is forever free to use?

forever is released under the MIT licence, as listed on its project page. The MIT licence allows free use, but check the licence text for the full terms.

How do I install forever?

Install it globally with npm using the command in this guide. You need Node.js and npm installed first. The README uses sudo in front of the command on systems where that is needed.

Can forever run scripts that are not written in Node.js?

Yes. The README says forever can run scripts whether they are written in node.js or not. Pass the script path to forever start.

How do I stop a script running in the background?

Use forever stop followed by the script name, for example forever stop my-daemon.js. To stop all scripts, use forever stopall.

Should I use forever or pm2 for a new project?

The README says that for new installations it encourages you to use pm2 or nodemon. It also says forever currently depends on the community for fixes and new features. Choose pm2 or nodemon for new projects.

Can I use forever inside my code?

The README says that if you use forever programmatically you should install forever-monitor instead. forever-monitor is the package it points to for code-level control.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

forever suits developers who already use it or need a simple way to keep a small Node.js script running and in the background. It is a poor fit for new projects, since the README recommends pm2 or nodemon and says fixes and features depend on the community. If you need programmatic control in code, use forever-monitor as the README directs, or pick pm2 or nodemon for a new setup.

Install options & downloads