javascript - node.js: delete require.cache not working -


i'm trying build hmr koa2 development chokidar.

changing text in ./middlewares/render triggers chokidar file-watch event, , require.cache cleared expected, when reload page, text rendered not changed.

./index.js

const path = require('path'); const http = require('http'); const koa = require('koa'); const chokidar = require('chokidar'); const views = require('./middlewares/views.js'); const render = require('./middlewares/render.js');  const port = 3000;  const app = new koa();  app.use(views);  app.use(render);  const server = http.createserver(app.callback());  server.listen(port, function() {   console.log('server listening on port: %s', port); });  const watcher = chokidar.watch([   path.resolve(__dirname, './middlewares'), ]);  watcher.on('ready', function() {   watcher.on('all', function(event, filename) {     console.log('clearing module cache...');         object.keys(require.cache).foreach(function (id) {           if (/[\/\\]middlewares[\/\\]/.test(id)) {             console.log('delete cache: %s', id);             delete require.cache[id];           }         });   }); }); 

./middlewares/views.js

const views = require('koa-views');  module.exports = views('./templates', {   http: 'ejs',   extension: 'ejs', }); 

./middlewares/render.js

module.exports = async (ctx, next) => {   // changing text, triggers chokidar file-watch event.   // require.cache seems cleared immediately.   // when reload page, text not changes!   const content = 'test delete require.cache.';    await ctx.render('main.ejs', { content: content }); }; 

./templates/index.ejs

<!doctype html> <html lang="en"> <head>   <title>node repo</title> </head> <body>   <div><%- content %></div> </body> </html> 

okay... repository helped.

previously

const views = require('./middlewares/views.js'); const render = require('./middlewares/render.js');  app.use(views); app.use(render); 

now

app.use(async (ctx, next) => {   const views = require('./middlewares/views.js');   await views(ctx, next); });  app.use(async (ctx, next) => {   const render = require('./middlewares/render.js');   await render(ctx, next); }); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -