Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 202
Shlomo Avatar asked Oct 20 '15 11:10

Shlomo


People also ask

Can be used to track the changes in the file in node JS?

To watch any changes that are made to the file system, we can use the fs module provided by node. js that provides a solution here. For monitoring a file of any modification, we can either use the fs. watch() or fs.

Where does node JS save files?

D:\myproject\subfoldername> node helloworld.js It is very easy.. Go to your command line. navigate to the file location.. then simply run the node helloworld.

How do I change the working directory in node JS?

You can change the current working directory of the Node process through the command process. chdir() . var process = require('process'); process. chdir('../');

What is writeFile in node JS?

writeFile() method is used to asynchronously write the specified data to a file. By default, the file would be replaced if it exists. The 'options' parameter can be used to modify the functionality of the method. Syntax: fs.writeFile( file, data, options, callback )


1 Answers

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.

like image 105
roskelld Avatar answered Sep 22 '22 23:09

roskelld