Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: how to reload module

I am new to NodeJS so probably I am doing some mistakes.

I have written a bunch of code in an external file called myapp. I start NodeJS for windows and from the interpreter window I type:

var myapp = require('d:/myapp.js'); 

then I can use my functions and variables in the external module.

The problem is that if I update the code in myapp then the interpreter does not re-read the file and it uses the old version.

Now, is this normal in the first place? How to work around this problem?

P.S.: I have spent hours in internet and searched in many forums including this. It was more confusing then anything else.

Thanks.

like image 322
az1mov Avatar asked Nov 05 '15 14:11

az1mov


People also ask

How do I load a node JS module?

Loading Core Modules As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns. The following example demonstrates how to use Node.

How do I restart node JS?

If it's just running (not a daemon) then just use Ctrl-C.

How do I fix a node module error?

If you have run the npm install command before, then it's possible that the installation of the module is incomplete or corrupted. Delete the node_modules/ folder using the rm -rf node_modules command, then run npm install again. That may fix the issue. Finally, the same error can happen when you require() a local .

How do I use Proxyquire?

Usage. Two simple steps to override require in your tests: add var proxyquire = require('proxyquire'); to top level of your test file. proxyquire(...) the module you want to test and pass along stubs for modules you want to override.

Can I use require to load a node module?

In Node.js using require to load a module will cache the result. If a module exports a constructed object it will be treated as a singleton, meaning it will only be constructed once While this is useful and performant for your application, it may not be ideal when performing testing.

How does the NodeJS require() function work?

We always use the NodeJS require () function to load modules in our projects. But, how does this actually work? According to NodeJS documentation, require () is "to require modules." That’s all it says. No kidding. So I decided to dig deeper, to know what is the magic behind this.

How to use a module in a JavaScript file?

For Javascript, files the function module._compile () is called. The module creates a wrapper to the code in the file. In turn, the wrapper passes the variables exports, require, and module. This is why the file can use the module.exports property to export itself. It can also use require () to use other modules.

How to reload a dependency from the cache?

This means, you can simply delete the cache key and require will reload the dependency! So how does it work. Lets take a look a simple module that returns an object. If we use require on this module, it will always return the same date since it loads the instance from the cache.


1 Answers

There are some answers here as suggested in the comments.

However they are not REPL friendly, and might even use extra modules.

Here is a one line solution that you can paste in your REPL, inspired by the discussion on the other question:

function nocache(module) {require("fs").watchFile(require("path").resolve(module), () => {delete require.cache[require.resolve(module)]})} 

The function will delete your module from the cache each time the file changes. To use it, just paste it in the REPL, call nocache("d:/myapp.js"), then use require normally.

> function nocache(module) {require("fs").watchFile(require("path").resolve(module), () => {delete require.cache[require.resolve(module)]})} > nocache("d:/myapp.js"); > var myapp = require("d:/myapp.js"); ...... > myapp = require("d:/myapp.js"); .... 
like image 66
mihai Avatar answered Sep 30 '22 14:09

mihai