node.jsexpresscachingproduction-environmentviewengine

ExpressJs doesn't cache views in production mode


I have a problem with ExpressJs not caching my views when NODE_ENV is set to production. I'm using the EJS template engine. Here is my code:

npm script

"start": "set NODE_ENV=production && node ./bin/www"

app.js

console.log(app.get('env')) => logs production
console.log(app.get('view cache')) => logs undefined
app.set('view cache', true)
console.log(app.get('view cache')) => logs true

So it seems that when I set the NODE_ENV environment variable to production ExpressJs is not enabling view cache, but when I enable it manually by calling app.set('view cache', true) it will. According to the ExpressJs it should enable view caching by default.

I did test if the caching works, but it only does when I enable it manually.


Solution

  • I figured it out. The problem is not with ExpressJs or with npm scripts, but with the shell command I used.

    When using a console.dir I saw that NODE_ENV was set to production with an extra space. Changing the npm script to "start": "set NODE_ENV=production&&node ./bin/www" did work.