Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs require returns same old file when requiring a second time after the file changed

So inside my nodejs server file I have the line:

tools=require("./tools.js");

The tools file contains functions and thelike that I change alot, so I figured instead of restarting the server everytime I change something, I could simply add some way for me to re-require the tools.js, and so I did. However now the problem is, when I start the program, change the tools.js and make it re-require it, it requires it again as if it was still in the state it was when it was first required. What?

Edit: I don't want to restart the app upon a file change, since that would be the same as restarting the server, which is what I want to prevent! So I need something that let's me re-require it, ignoring module caching or whatever.

Any ideas what could help me here?

like image 844
Wingblade Avatar asked Oct 28 '12 12:10

Wingblade


People also ask

For what require () is used in Node JS?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Does NodeJS cache require?

Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.

How do I reload node server?

Live Reload Frontend along with node server: To do this we are going to use livereload package. Fire up the terminal and run npm install livereload. Now inside your main server file In my case I have server. js Inside this file, we have to require livereload package and then reload(your_server_var); function.

Which object is used to manage the cache of required modules NodeJS?

Module. _load performs the loading of new modules and manages the cache. On a file loading request, it will first check the file in cache. If module is not found in the cache, this will create a new base module instance for the required file.


5 Answers

If you don't want to restart the app do this every time you require it.

var path = require('path');
var filename = path.resolve('./tools.js');
delete require.cache[filename];
var tools = require('./tools');
like image 87
Teemu Ikonen Avatar answered Oct 26 '22 14:10

Teemu Ikonen


This solved it for me:

let tools = require("./tools.js"); 
delete require.cache[require.resolve("./tools.js")];
tools = require("./tools.js");

Creds to this answer

like image 33
milmal Avatar answered Oct 26 '22 13:10

milmal


If I understood you correctly, you're running into Node's module caching. http://nodejs.org/docs/latest/api/modules.html#modules_caching

You might want to look into a proper reloader for Node instead, if that's an option. See https://github.com/isaacs/node-supervisor for instance.

like image 43
AKX Avatar answered Oct 26 '22 15:10

AKX


Have a look at supervisor. Supervisor can restart the application automatically upon file changes and is simple to setup.

npm install -g supervisor

supervisor app.js

like image 35
Menztrual Avatar answered Oct 26 '22 14:10

Menztrual


Once node.js require()s a module, any subsequent call to require() fetches it from memory, the actual module file doesn't get loaded again. What you need to do is use a tool like nodemon (https://github.com/remy/nodemon ), which automatically restarts your app when files are changed.

like image 28
Victor Stanciu Avatar answered Oct 26 '22 13:10

Victor Stanciu