How to auto-reload files in Node.js?
Asked Answered
B

31

574

Any ideas on how I could implement an auto-reload of files in Node.js? I'm tired of restarting the server every time I change a file. Apparently Node.js' require() function does not reload files if they already have been required, so I need to do something like this:

var http    = require('http'),
    posix   = require('posix'),
    json    = require('./json');

var script_name = '/some/path/to/app.js';
this.app = require('./app').app;

process.watchFile(script_name, function(curr, prev){
    posix.cat(script_name).addCallback(function(content){
        process.compile( content, script_name );
    });
});

http.createServer(this.app).listen( 8080 );

And in the app.js file I have:

var file = require('./file');
this.app = function(req, res) { 
    file.serveFile( req, res, 'file.js');  
}

But this also isn't working - I get an error in the process.compile() statement saying that 'require' is not defined. process.compile is evaling the app.js, but has no clue about the node.js globals.

Beitris answered 29/12, 2009 at 0:8 Comment(3)
You know you can just run this code on each request: Object.keys(require.cache).forEach(function(key) { delete require.cache[key]; });Celebrity
See strongloop.com/strongblog/…Xymenes
Why do you have two js files, app.js as a server file should be enough?Chicory
N
815

A good, up to date alternative to supervisor is nodemon:

Monitor for any changes in your node.js application and automatically restart the server - perfect for development

To use nodemon with version of Node without npx (v8.1 and below, not advised):

$ npm install nodemon -g
$ nodemon app.js

Or to use nodemon with versions of Node with npx bundled in (v8.2+):

$ npm install nodemon
$ npx nodemon app.js

Or as devDependency in with an npm script in package.json:

"scripts": {
  "start": "nodemon app.js"
},
"devDependencies": {
  "nodemon": "..."
}
Nochur answered 18/1, 2013 at 19:17 Comment(10)
and if you want to use it in Nitrous.io -- $ nodemon -L yourfile.js (full explanation at coderwall.com/p/aqxl_q)Priapic
But in this case, it restart the server process as well.Effy
@Filipe, you are right. I am redirected to login again. I wish it only loaded that specific modified module.Bedspread
automatically restart the server - perfect for development is way too much hyperbole. Reloading server could mean logging into backend services which does take a long time in my case. "Perfect for development" would be something like hot-reloading classes while process is running in memory without losing state a-la what android studio does when you change source code.Greyback
use npm install [--save-dev | -D] nodemon to limit the installation to project scope.Schopenhauer
This only restarts the server, the web clients would still need to be manually reloaded.Magnetize
after running nodemon app.js my app no longer loads via localhost:3000Piane
is there a way to reload a json file if it changed without restarting the node server?Ollieollis
An old answer, but: given the question, which suggests that this person wish that require didn't cache, the answer really was (and still is) to update the require function to bypass cache when running in develop mode. Not "restarting the server every time a file has any kind of change, no matter how trivial". E.g. stackoverflow.com/questions/9210542Haily
I wrote a vite plugin to enable nodejs HMR without restart server on file updated. github.com/axe-me/vite-plugin-nodeBorchardt
U
325

node-supervisor is awesome

usage to restart on save for old Node versions (not advised):

npm install supervisor -g
supervisor app.js

usage to restart on save for Node versions that come with npx:

npm install supervisor
npx supervisor app.js

or directly call supervisor in an npm script:

"scripts": {
  "start": "supervisor app.js"
}
Ubald answered 5/2, 2011 at 14:2 Comment(9)
npm install -g supervisor. It should be installed globally.Benbena
On OSx 10.2.8 i had to run it with sudoLeeway
Had to run it like this under Windows: "C:\Program Files\nodejs\node.exe" C:\Users\Mark\AppData\Roaming\npm\node_modules\supervisor\lib\cli-wrapper.js app.jsDannielledannon
without -g or sudo at app root: npm install supervisor, node node_modules/supervisor/lib/cli-wrapper.js app.js (I have a non-root installation of Node)Sideswipe
@Mark This means node is not in your PATHGrosgrain
To make supervisor watch only for specific files supervisor -w <file1>,<file2> -x node <serverexecutable>, example: supervisor -w app.js,routes/index.js -x node bin/wwwInelegance
i installed supervisor on ubuntu, but when i run something say supervisor app.js, it says supervisor command not found. Kindly help, I faced same issue on nodemon, nodev :/Yvoneyvonne
This was burning up CPU for me, but nodemon wasn't.Swafford
Note that this is very much not what the question was about though. Restarting is the exact opposite of hot-reloading.Haily
G
106

i found a simple way:

delete require.cache['/home/shimin/test2.js']
Goa answered 25/2, 2011 at 7:45 Comment(7)
This is great if you want to reload external libraries without restarting the app--in my case, an IRC bot.Nonplus
This is excellent! So simple and works so well. Whenever a request comes in I just uncache a bunch of files that don't hold state.Ranaerancagua
delete require.cache[require.resolve('./mymodule.js')]; resolve deal with real pathsRetroactive
Is this safe to do or considered "bad practice" or "development only"?Socialistic
@Socialistic I don't think it's safe, since it may recreate classes and functions or whatever exports, resulting in different references when comparing with ===Triggerhappy
I'd point out this reload not only the logics, but also file level states, e.g. export let/var/constBeeswing
Note that this does not update any code that is already running on the code file's content, so really isn't hot reloading so much as "cache invalidation".Haily
S
27

If somebody still comes to this question and wants to solve it using only the standard modules I made a simple example:

var process = require('process');
var cp = require('child_process');
var fs = require('fs');

var server = cp.fork('server.js');
console.log('Server started');

fs.watchFile('server.js', function (event, filename) {
    server.kill();
    console.log('Server stopped');
    server = cp.fork('server.js');
    console.log('Server started');
});

process.on('SIGINT', function () {
    server.kill();
    fs.unwatchFile('server.js');
    process.exit();
});

This example is only for one file (server.js), but can be adapted to multiple files using an array of files, a for loop to get all file names, or by watching a directory:

fs.watch('./', function (event, filename) { // sub directory changes are not seen
    console.log(`restart server`);
    server.kill();
    server = cp.fork('server.js');    
})

This code was made for Node.js 0.8 API, it is not adapted for some specific needs but will work in some simple apps.

UPDATE: This functional is implemented in my module simpleR, GitHub repo

Straightlaced answered 13/9, 2012 at 17:30 Comment(3)
This is a great and simple solution. I just used it for a bot that was supposed to update itself from git when told so by a moderator. The problem was that once you're inside the app you can't restart yourself. I can, however, use your method to spawn an instance of the bot and watch a dotfile. The bot then updates itself, touches the dotfile, and will be automatically restarted by the launcher. Awesome!Dilettante
@Dilettante i'm glad to hear this :) I will implement this solution in a module, soon I guess, I have some more ideas how to expand its functionalityStraightlaced
If file watch is not needed, reload can be done without fs, by listening different signal.Outlawry
C
26

Nodemon has been the go to for restarting server for file changes for long time. Now with Node.js 19 they have introduced a --watch flag, which does the same [experimental]. Docs

node --watch index.js
Contortive answered 23/10, 2022 at 7:46 Comment(0)
S
20

nodemon came up first in a google search, and it seems to do the trick:

npm install nodemon -g
cd whatever_dir_holds_my_app
nodemon app.js
Sain answered 4/12, 2011 at 3:3 Comment(0)
P
12

nodemon is a great one. I just add more parameters for debugging and watching options.

package.json

  "scripts": {
    "dev": "cross-env NODE_ENV=development nodemon --watch server --inspect ./server/server.js"
  }

The command: nodemon --watch server --inspect ./server/server.js

Whereas:

--watch server Restart the app when changing .js, .mjs, .coffee, .litcoffee, and .json files in the server folder (included subfolders).

--inspect Enable remote debug.

./server/server.js The entry point.

Then add the following config to launch.json (VS Code) and start debugging anytime.

{
    "type": "node",
    "request": "attach",
    "name": "Attach",
    "protocol": "inspector",
    "port": 9229
}

Note that it's better to install nodemon as dev dependency of project. So your team members don't need to install it or remember the command arguments, they just npm run dev and start hacking.

See more on nodemon docs: https://github.com/remy/nodemon#monitoring-multiple-directories

Parasite answered 29/1, 2018 at 11:33 Comment(3)
Globbing is not supported for recent versions of nodemon (1.19.0 at least). Simply use nodemon --watch server --inspect ./server/server.js instead.Antidepressant
Thanks @Antidepressant for your information. Updated the answer.Parasite
is there a way to rebuild the app with a rebuild for any changes to any client-side files in the react app?Crupper
L
8

There is Node-Supervisor that you can install by

npm install supervisor

see http://github.com/isaacs/node-supervisor

Larcher answered 28/9, 2010 at 17:1 Comment(2)
It's more about restarting the server if it crashes. node-supervisor also restarts the whole process when watched files have been changed. It is not hot-reload in the strict sense.Schaefer
Although not really hot-loading, this tool is really useful if you just want the code to autoreload while you're developing so you don't have to restart node in the command line after every change.Stuckey
S
8

node-dev works great. npm install node-dev

It even gives a desktop notification when the server is reloaded and will give success or errors on the message.

start your app on command line with:

node-dev app.js

Sennet answered 16/2, 2017 at 16:20 Comment(0)
C
8

You can use nodemon from NPM. And if you are using Express generator then you can using this command inside your project folder:

nodemon npm start

or using Debug mode

DEBUG=yourapp:* nodemon npm start

you can also run directly

nodemon your-app-file.js

Hope this help.

Carbineer answered 8/9, 2018 at 3:31 Comment(3)
this works on Windows if you type set DEBUG=myapp:* & nodemon npm startPiane
I also had to follow these instructions to install nodemon globally https://mcmap.net/q/74310/-nodemon-command-is-not-recognized-in-terminal-for-node-js-serverPiane
I used express generator and nodemon npm start really worked, thanksScow
C
6

There was a recent (2009) thread about this subject on the node.js mailing list. The short answer is no, it's currently not possible auto-reload required files, but several people have developed patches that add this feature.

Coldhearted answered 31/12, 2009 at 1:19 Comment(1)
+1 Yes. I participated in the discussion. I admitted that my solution is too simple. It only works if the hot module itself does not require further modules. Felix' solution is more well thought-out but it is debated if auto-reload really belongs to the core.Schaefer
A
6

With Node.js 19 you can monitor file changes with the --watch option. After a file is changed, the process is restarted automatically, reflecting new changes.

node --watch server.js
Anorthite answered 19/10, 2022 at 8:13 Comment(0)
C
4

yet another solution for this problem is using forever

Another useful capability of Forever is that it can optionally restart your application when any source files have changed. This frees you from having to manually restart each time you add a feature or fix a bug. To start Forever in this mode, use the -w flag:

forever -w start server.js
Cracow answered 16/3, 2015 at 16:43 Comment(2)
Strangely with the -w flag my express.js app doesn't use CSS.Roadbed
'forever' is not recognized as an internal or external command, operable program or batch file.Piane
M
3

Here is a blog post about Hot Reloading for Node. It provides a github Node branch that you can use to replace your installation of Node to enable Hot Reloading.

From the blog:

var requestHandler = require('./myRequestHandler');

process.watchFile('./myRequestHandler', function () {
  module.unCacheModule('./myRequestHandler');
  requestHandler = require('./myRequestHandler');
}

var reqHandlerClosure = function (req, res) {
  requestHandler.handle(req, res);
}

http.createServer(reqHandlerClosure).listen(8000);

Now, any time you modify myRequestHandler.js, the above code will no­tice and re­place the local re­questHandler with the new code. Any ex­ist­ing re­quests will con­tin­ue to use the old code, while any new in­com­ing re­quests will use the new code. All with­out shut­ting down the serv­er, bounc­ing any re­quests, pre­ma­ture­ly killing any re­quests, or even re­ly­ing on an in­tel­li­gent load bal­ancer.

Micturition answered 3/10, 2010 at 21:32 Comment(1)
The only thing with this solution is that it's a fork of an older version of Node, so it will have to be tweaked and merged with the latest version before using (unless you don't mind using an older version of Node).Micturition
P
3

solution at: http://github.com/shimondoodkin/node-hot-reload

notice that you have to take care by yourself of the references used.

that means if you did : var x=require('foo'); y=x;z=x.bar; and hot reloaded it.

it means you have to replace the references stored in x, y and z. in the hot reaload callback function.

some people confuse hot reload with auto restart my nodejs-autorestart module also has upstart integration to enable auto start on boot. if you have a small app auto restart is fine, but when you have a large app hot reload is more suitable. simply because hot reload is faster.

Also I like my node-inflow module.

Puce answered 5/10, 2010 at 1:51 Comment(2)
That's a nice one!Plinth
this is old one sorryPuce
F
3

I am working on making a rather tiny node "thing" that is able to load/unload modules at-will (so, i.e. you could be able to restart part of your application without bringing the whole app down). I am incorporating a (very stupid) dependency management, so that if you want to stop a module, all the modules that depends on that will be stopped too.

So far so good, but then I stumbled into the issue of how to reload a module. Apparently, one could just remove the module from the "require" cache and have the job done. Since I'm not keen to change directly the node source code, I came up with a very hacky-hack that is: search in the stack trace the last call to the "require" function, grab a reference to it's "cache" field and..well, delete the reference to the node:

    var args = arguments
    while(!args['1'] || !args['1'].cache) {
        args = args.callee.caller.arguments
    }
    var cache = args['1'].cache
    util.log('remove cache ' + moduleFullpathAndExt)
    delete( cache[ moduleFullpathAndExt ] )

Even easier, actually:

var deleteCache = function(moduleFullpathAndExt) {
  delete( require.cache[ moduleFullpathAndExt ] )
}

Apparently, this works just fine. I have absolutely no idea of what that arguments["1"] means, but it's doing its job. I believe that the node guys will implement a reload facility someday, so I guess that for now this solution is acceptable too. (btw. my "thing" will be here: https://github.com/cheng81/wirez , go there in a couple of weeks and you should see what I'm talking about)

Feed answered 22/2, 2011 at 15:6 Comment(3)
..of course is not that simple. That only works if there is a call to require in the call stack. Oh well, easy hack on top of an hack: write that stuff in a temp script, and require it at runtime. Did it, it works..and it even clean itself from the cacheFeed
And actually it was easier: delete( require.cache[moduleFullpathAndExt] )Feed
Node.js modules are actually wrapped in an anonymous function which is how the module encapsulation is done. Each module actually looks like function (module, require) { /* your code */ }. When you take this into account, arguments[1] points at require. And the while loop is there for situations where you call this from within another function in a module (it simply goes up the function hierarchy and checks the argument values passed to each).Margo
A
2

Here's a low tech method for use in Windows. Put this in a batch file called serve.bat:

@echo off

:serve
start /wait node.exe %*
goto :serve

Now instead of running node app.js from your cmd shell, run serve app.js.

This will open a new shell window running the server. The batch file will block (because of the /wait) until you close the shell window, at which point the original cmd shell will ask "Terminate batch job (Y/N)?" If you answer "N" then the server will be relaunched.

Each time you want to restart the server, close the server window and answer "N" in the cmd shell.

Alderson answered 24/11, 2018 at 0:26 Comment(0)
G
2

my app structure:

NodeAPP (folder)
   |-- app (folder)
      |-- all other file is here
   |-- node_modules (folder)
   |-- package.json
   |-- server.js (my server file)

first install reload with this command:

npm install [-g] [--save-dev] reload

then change package.json:

"scripts": {
    "start": "nodemon -e css,ejs,js,json --watch app"
}

now you must use reload in your server file:

var express = require('express');
var reload = require('reload');
var app = express();

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
    console.log( 'server is running on port ' + app.get('port'));
});

reload(server, app);

and for last change, end of your response send this script:

<script src="/reload/reload.js"></script>

now start your app with this code:

npm start
Gariepy answered 8/3, 2019 at 12:0 Comment(3)
This approach doesn't work, however, the ones led out in npmjs.com/package/reload (for Express apps) do.Magnetize
This method works well and I use this method in a project that you can see in this path: github.com/KasraK2K/imensite-tutorial-siteGariepy
The reload(server, app); is wrong, you have to change to reload(server);. In your code on github, you got it. Furthermore, I get an error error TS7016: Could not find a declaration file for module 'reload' and will open an issue because of typescript.Chicory
S
2

You can do it with browser-refresh. Your node app restarts automatically, your result page in browser also refreshes automatically. Downside is that you have to put js snippet on generated page. Here's the repo for the working example.

const http = require('http');
const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html; charset=UTF-8');
    res.write('Simple refresh!');
    res.write(`<script src=${process.env.BROWSER_REFRESH_URL}></script>`);
    res.end();
})

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);

    if (process.send) {
        process.send({ event: 'online', url: `http://${hostname}:${port}/` })
    }

});
Santa answered 22/12, 2019 at 13:36 Comment(0)
A
1

Not necessary to use nodemon or other tools like that. Just use capabilities of your IDE.

Probably best one is IntelliJ WebStorm with hot reload feature (automatic server and browser reload) for node.js.

Alerion answered 8/6, 2016 at 8:43 Comment(0)
C
1

I have tried pm2 : installation is easy and easy to use too; the result is satisfying. However, we have to take care of which edition of pm2 that we want. pm 2 runtime is the free edition, whereas pm2 plus and pm2 enterprise are not free.

As for Strongloop, my installation failed or was not complete, so I couldn't use it.

Cabal answered 8/4, 2020 at 15:36 Comment(0)
L
1

If your talking about server side NodeJS hot-reloading, lets say you wish to have an Javascript file on the server which has an express route described and you want this Javascript file to hot reload rather than the server re-starting on file change then razzle can do that.

An example of this is basic-server

https://github.com/jaredpalmer/razzle/tree/master/examples/basic-server

The file https://github.com/jaredpalmer/razzle/blob/master/examples/basic-server/src/server.js will hot-reload if it is changed and saved, the server does not re-start.

This means you can program a REST server which can hot-reload using this razzle.

Lippert answered 25/6, 2021 at 1:11 Comment(0)
D
1

it's quite simple to just do this yourself without any dependency... the built in file watcher have matured enough that it dose not sucks as much as before

you don't need any complicated child process to spawn/kill & pipe std to in/out... you just need a simple web worker, that's all! A web Worker is also what i would have used in browsers too... so stick to web techniques! worker will also log to the console

import { watch } from 'node:fs/promises'
import { Worker } from 'node:worker_threads'

let worker = new Worker('./app.js')

async function reloadOnChange (dir) {
  const watcher = watch(dir, { recursive: true })
  for await (const change of watcher) {
    if (change.filename.endsWith('.js')) {
      worker.terminate()
      worker = new Worker('./app.js')
    }
  }
}

// All the folder to watch for
['./src', './lib', './test'].map(reloadOnChange)

this might not be the best solution where you use anything else other than javascript and do not depend on some build process.

Delusive answered 6/9, 2022 at 12:19 Comment(0)
A
0

Use this:

function reload_config(file) {
  if (!(this instanceof reload_config))
    return new reload_config(file);
  var self = this;

  self.path = path.resolve(file);

  fs.watchFile(file, function(curr, prev) {
    delete require.cache[self.path];
    _.extend(self, require(file));
  });

  _.extend(self, require(file));
}

All you have to do now is:

var config = reload_config("./config");

And config will automatically get reloaded :)

Ac answered 26/12, 2013 at 19:35 Comment(1)
Got a version that doesn't rely on a framework that isn't part of Node?Limitary
L
0

loaddir is my solution for quick loading of a directory, recursively.

can return

{ 'path/to/file': 'fileContents...' } or { path: { to: { file: 'fileContents'} } }

It has callback which will be called when the file is changed.

It handles situations where files are large enough that watch gets called before they're done writing.

I've been using it in projects for a year or so, and just recently added promises to it.

Help me battle test it!

https://github.com/danschumann/loaddir

Leisurely answered 25/3, 2014 at 20:52 Comment(0)
G
0

You can use auto-reload to reload the module without shutdown the server.

install

npm install auto-reload

example

data.json

{ "name" : "Alan" }

test.js

var fs = require('fs');
var reload = require('auto-reload');
var data = reload('./data', 3000); // reload every 3 secs

// print data every sec
setInterval(function() {
    console.log(data);
}, 1000);

// update data.json every 3 secs
setInterval(function() {
    var data = '{ "name":"' + Math.random() + '" }';
    fs.writeFile('./data.json', data);
}, 3000);

Result:

{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: 'Alan' }
{ name: '0.8272748321760446' }
{ name: '0.8272748321760446' }
{ name: '0.8272748321760446' }
{ name: '0.07935990858823061' }
{ name: '0.07935990858823061' }
{ name: '0.07935990858823061' }
{ name: '0.20851597073487937' }
{ name: '0.20851597073487937' }
{ name: '0.20851597073487937' }
Genro answered 19/9, 2014 at 3:24 Comment(0)
A
0

another simple solution is to use fs.readFile instead of using require you can save a text file contaning a json object, and create a interval on the server to reload this object.

pros:

  • no need to use external libs
  • relevant for production (reloading config file on change)
  • easy to implement

cons:

  • you can't reload a module - just a json containing key-value data
Ancestor answered 25/11, 2014 at 8:18 Comment(0)
R
0

For people using Vagrant and PHPStorm, file watcher is a faster approach

  • disable immediate sync of the files so you run the command only on save then create a scope for the *.js files and working directories and add this command

    vagrant ssh -c "/var/www/gadelkareem.com/forever.sh restart"

where forever.sh is like

#!/bin/bash

cd /var/www/gadelkareem.com/ && forever $1 -l /var/www/gadelkareem.com/.tmp/log/forever.log -a app.js
Rachellrachelle answered 29/11, 2014 at 14:12 Comment(0)
O
0

I recently came to this question because the usual suspects were not working with linked packages. If you're like me and are taking advantage of npm link during development to effectively work on a project that is made up of many packages, it's important that changes that occur in dependencies trigger a reload as well.

After having tried node-mon and pm2, even following their instructions for additionally watching the node_modules folder, they still did not pick up changes. Although there are some custom solutions in the answers here, for something like this, a separate package is cleaner. I came across node-dev today and it works perfectly without any options or configuration.

From the Readme:

In contrast to tools like supervisor or nodemon it doesn't scan the filesystem for files to be watched. Instead it hooks into Node's require() function to watch only the files that have been actually required.

Od answered 12/12, 2014 at 17:45 Comment(0)
F
0
const cleanCache = (moduleId) => {
    const module = require.cache[moduleId];
    if (!module) {
        return;
    }
    // 1. clean parent
    if (module.parent) {
        module.parent.children.splice(module.parent.children.indexOf(module), 1);
    }
    // 2. clean self
    require.cache[moduleId] = null;
};
Fruin answered 20/5, 2019 at 9:21 Comment(0)
W
-1

Nowadays WebPack dev server with hot option is used. you can add a script like this in your package.json : "hot": "cross-env NODE_ENV=development webpack-dev-server --hot --inline --watch-poll",

and every change in your files will trigger a recompile automatically

Walkon answered 27/3, 2018 at 12:44 Comment(1)
This answer is wrong wrt the question. Webpack is for frontend applications and the dev server is a webserver on its own. The question was referring to a server application implemented in Node. It does not need a webserver. It is one already.Louth

© 2022 - 2024 — McMap. All rights reserved.