I get that require caches a variable and it is the same for every request. How to make require to create a new object every time?

let Handlebars = require('handlebars'); console.log(Handlebars.count); // 50 Handlebars.count = 0; Handlebars.count = 50; 
  • require loads a module. A module is part of your program. How can part of a working program change? - Pavel Mayorov
  • Caches and it is written in the documentation. - Alexey Ten
  • It is better to write the problem you are trying to solve, and we will try to find the right solution. - Alexey Ten
  • Found a solution. Handlebars have a create method. It does not create a global instance. And by default it is global. var GlobalHandlebars = require ('handlebars'); var Handlebars = GlobalHandlebars.create (); - manking
  • If our solution, then add it as an answer. (You’re probably used to PHP, where every request starts anew. In the case of Node.js, your program runs once and runs all the time while it processes requests. For example, thanks to this you can theoretically see what other request does from one request. ) - sanmai

1 answer 1

Require is cached, you can see it in require.cache. But to touch this cache is not recommended. In this case, it is more correct to create a new instance of handlebars, rather than use a global object.

 let GlobalHandlebars = require('handlebars'); let Handlebars = GlobalHandlebars.create();