I have node server which i run using forever. But each time if I edit my code I'll have to restart the server. I came across the module called hotnode which can perform live edits but will it have the same performance as the forever module or can I run my code using both the modules. I am confused. Any help wil be much helpful
Have a look at nodemon.
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
As an alternative to nodemon you can use node-supervisor.
I used to use nodemon, but for some reason it didn't detect code changes on my linux box, which supervisor did flawlessly.
The downside is that it doesn't (or at least didn't) give the colorful output nodemon gives.
Related
I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.
How can I run hapijs as a server deamon on a Linux box? Right now, I'm running it as a user process for development with the node index.js command for the main page, but in the long run it should be www-data or whatever else user that runs the process.
If you want to run node as a daemon without any extra tools, you can use nohup:
nohup node index.js &
However, the following tools can do this and also have some other really useful features such as automatic restart on exit, log redirection and in the case of PM2, clustering:
PM2: https://github.com/Unitech/pm2
Forever: https://github.com/foreverjs/forever
If you want your service to start when your machine start/reboots, you can use something like Upstart (on ubuntu) or System-V:
https://www.digitalocean.com/community/tutorials/how-to-write-a-linux-daemon-with-node-js-on-a-vps
To run as different user to the user you're logged in with:
sudo -u somebody node index.js
Please note that none of the above is specific to hapi but rather applies to any Node.js app.
PM2 is the best option hands down. It scales from local development through to production without issue.
First Step:
npm install -g pm2
The -g flag is simply for installing to globally so it's available as system command.
Second Step:
pm2 start index.js
The start command simply replaces node index.js Behind the scenes it runs the node process but as a daemon.
PM2 Actual Use Case
cd projects/my-app
npm install -g pm2
npm install
NODE_ENV=development pm2 start index.js -n my-app
pm2 stop my-app
pm2 restart my-app
pm2 status
pm2 logs my-app
pm2 m
Those should be enough to get you going. The nice thing about PM2 is it works great in a CI/CD environment as well since you can recall process by name. Finally out of the box it does log rotation and a few other awesome things to keep you going even if stuff goes south. Apps will also auto restart if they crash (obv. configurable).
Additional configuration allows PM2 to watch files on disk and restart the app as they change. This is great for development as you can code + save files and the API you're building in HapiJS will simply restart and your changes are live.
I use supervisord and it works great.
In short you have to configure supervisord to start your hapijs application. In addition you need to configure nginx or apache to reverse proxy requests to your hapijs application.
You can find detailed instructions on set up at http://blog.risingstack.com/operating-node-in-production/
It feels odd to suggest a tool when you haven't explicitly asked for one. nohup-ing the process and running in the background is an option that requires no new tooling, but for what it's worth, I would suggest Docker-izing the application and letting docker handle everything. Docker has several features built in, and even though it is not just for creating a daemon (it does so much more), you can use it's restart='always' feature to keep a process running.
Hope that helps.
I'm new to *Hapi *framework. During development, I have to restart the hapi server whenever I made any changes to the code.
For the view part, I can add an option {isCached: false}to make the view read the latest html file every time. However, is there an easy setting to make it reload code automatically whenever it is changed?
UPDATE:
Thanks to dylants' suggestion, Nodemon works great.
However, in my app there is a selenium-standalone child process, whenever the nodemon restarts, it will generate an error log. ...Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again...
I have tried https://github.com/remy/nodemon#controlling-shutdown-of-your-script, it doesn't help.
I've used nodemon. You just start your server with $ nodemon instead of $ npm start and every time you make a change to your server code it restarts the server.
I have found node-dev to work well for me.
npm install -g node-dev
I personally prefer pm2 to achieve this.
pm2 start app --watch
More info about pm2: http://pm2.keymetrics.io/
I just took a look to this tutorial: How can I run nodemon from within WebStorm?
I'm wondering how reload only the files updated and don't restart the entire server instance.
I'm wondering also if it's possible to don't break a debug breakpoint, because for the moment the entire server is restarted and if I'm following instruction step by step using the debogguer, I lost the track.
By the way, there are my ways to use them with webstorm, it's particularly tricky with supervisor. Because it needs to get the app.js and everything as parameters.
Supervisor: https://docs.google.com/file/d/0ByzbHcAxmCyvTGdHOWJSTTYzNWs/edit
Nodemon: https://docs.google.com/file/d/0ByzbHcAxmCyvdVQ3azFhSlV0dEU/edit
You can use modules like hotswap to reload files when they change.
It can cause memory leaks and other stuff, but it's working fine for development.
It's a fairly small thing, but it feels like I'm manually refreshing the node-inspector tab in chrome a million times a day, and there must be a better way.
When a file changes, and node restarts, and node-inspector detaches from target -- is there a way for it to automatically re-attach itself?
This question is a duplicate of How can I make node-inspector restart when node is restarted?. See the accepted answer for a workaround solution using GreaseMonkey.
There is also a GitHub issue filled in the Node Inspector project: #266.
Here's instructions to have a Node console (not REPL), while using nodemon, with a server output console, all from within VSCode.
Node.js debugging in VS Code with Nodemon
The only thing to look out for is that it needs to be started manually with
nodemon --inspect ./bin/www
You can't let nodemon use your package.json start defaults because it won't restart.
The only thing this lacks is a webpage restart (if you're using web front end) but that's a whole other question.