Shlomo
Shlomo

Reputation: 3990

forever node.js - watch directory for file changes

I want my node application index.js to be restarted if there is any file change detected in its directory or below. Additionally, I want the process to be in the foreground, outputting logs to the terminal. What is the command?

My tries:

forever stopall

forever -w /home/patrick/workspace/frontend-api/index.js

Result:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Could not read .foreverignore file.
error: ENOENT, open '/.foreverignore'
error: restarting script because unlinkDir changed

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: watch EACCES
    at errnoException (fs.js:1024:11)
    at FSWatcher.start (fs.js:1056:11)
    at Object.fs.watch (fs.js:1081:11)

Upvotes: 8

Views: 9316

Answers (3)

Dave Sag
Dave Sag

Reputation: 13486

As explained by @stuffyjoelab forever -w checks for a .foreverignore file, and will get stuck if it's not there.

Here's my simple approach to adding forever to a node server project.

  1. create .foreverignore

    # we only care about changes to javascript files
    # in the src folder
    !src/*.js
    
  2. npm i -D forever to add forever as a dev-dependency

  3. add this to the scripts in your package.json file

    "dev": "forever -w --minUptime=1000 --spinSleepTime=1000 index.js",
    
  4. npm run dev to start it up with forever.

  5. Change a file and voila — your server reboots

Upvotes: 0

roskelld
roskelld

Reputation: 1130

From what I understand (the documentation on this is non-existent from what I can see). the -w or --watch function makes forever check for a .foreverignore file, and if this is missing the program essentially fails and gets stuck in a loop never starting the module.

in your app directory create a file called .foreverignore and list everything that you do not want forever to watch. This basically tells forever to ignore changes to these files and not to restart if anything happens to them, which is great for log files or things that don't actually require your module to restart to get the benefits from.

Here's an example taken from my implementation:

File:

/apps/myapp/.foreverignore

Contents

node_modules/*
logs/*
conf/*
htdocs/*
*.log
*.gif
*.jpg
*.html

Once created make sure you restart forever for the file to be picked up.

Upvotes: 9

Aishwat Singh
Aishwat Singh

Reputation: 4459

seems like u need nodemon

npm install -g nodemon

however in my windows server i used https://github.com/tjanczuk/iisnode good for scalability on multi core servers

Upvotes: 5

Related Questions