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)
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.
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.
You can change the current working directory of the Node process through the command process. chdir() . var process = require('process'); process. chdir('../');
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 )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With