Why server restart is needed in Node.Js for every change? - node.js

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.

Related

Waiting for webpack bundling before launching nodemon

I have a nodejs project written in Typescript. Therefore, i have webpack using a typescript loader that transpiles my code in Javascript and bundles it in a server.js file (in a dist folder)
When in developement conditions, my webpack runs with its watcher ON and so does nodemon.
Problem is, when i launch my script for the first time combining webpack and nodemon, since webpack is in watch mode it doesn't have an exit code saying that everything is ok, nodemon script can be started. If i run them simultaneously, nodemon will launch faster than webpack and since server.js file doesn't yet exist, it will crash at the start.
I want to launch thanks to one single command both scripts but make nodemon command wait for the bundling to be done.
First of all, when please provide some code when submitting questions.
and since server.js file doesn't yet exist
I think you should work around your setup a little bit s.t. webpack doesn't create your server.js file if you want to do this.
Basically you can chain multiple commands in a script like so webpack -d && nodemon index.js. This will launch node after webpack completes. However if you setup webpack in watch mode -w it never exists, so you can't chain another command to it. So webpack -d -w && nodemon index.js never gets to the nodemon part.
A solution to the above is to chain them using only &, which I guess you are doing, but in this way they both start at the same time. Hence, if you make your setup independent (webpack doesn't interfere with nodemon starting script) you can list them like so.
If for whatever reasons you can't do this or don't want to, your only option is with 2 separate scripts that you launch manually one after the other.
If I were you, I would just use nodemon-webpack-plugin:
Uses Nodemon to watch and restart your module's output file, but only
when webpack is in watch mode (ie, --watch).
Saves the need for installing, configuring and running Nodemon as a
seperate process.

How to make Hapi auto reload app during developing

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/

Use nodemon or supervisor to reload only the files without restart all the server - WebStorm

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.

Is there a way to automatically reload node-inspector when server restarts?

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.

How to edit and deploy code without restarting server?

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.

Resources