2

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.

Tarabass
  • 3,132
  • 2
  • 17
  • 35

2 Answers2

0

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.

Heartbit
  • 1,698
  • 2
  • 14
  • 26
  • Did you mean running `set NODE_ENV=production && node ./bin/www` from the CLI? I did that but unfortunately with the same result. I'm on Windows, so the code you provided will not work.. – Tarabass Jan 01 '17 at 17:17
  • When you run from CLI you don't need 'set' and '&'. – Heartbit Jan 01 '17 at 19:37
  • A error is thrown when I don't use them: `'NODE_ENV' is not recognized as an internal or external command, operable program or batch file. ` – Tarabass Jan 01 '17 at 19:40
  • 1
    Did you see here http://stackoverflow.com/questions/9249830/how-can-i-set-node-env-production-in-windows – Heartbit Jan 01 '17 at 20:01
  • Now I have. As you can read there I have to use 'set' and '&&' on Windows, but without spaces. I'm not on unix. I don't get it that node is not trimming the value of the variable to avoid this. Also I know that it isn't a good idea to set an environment variable hardcoded in package.json, I only wanted to test production. `npm run env NODE_ENV=production` did not work at all for me.. – Tarabass Jan 01 '17 at 20:11
0

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.

Tarabass
  • 3,132
  • 2
  • 17
  • 35