ExpressJs doesn't cache views in production mode

1.3k views Asked by At

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.

2

There are 2 answers

0
Tarabass On BEST ANSWER

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.

5
Heartbit On

First run your app by NODE_ENV=production node app.js and if its true so you should change your start attribute in package.json.