Search code examples
node.jsexpresstemplatesnunjucks

How do access NODE_ENV in Nunjucks template?


I have some Nunjucks template blocks that I want to render only in certain environments. I can't seem to access the NODE_ENV variable though. I tried this:

{% if process.env.NODE_ENV === 'development' %}
  <div>rendering some stuff here</div>
{% endif %}

This didn't seem to work for me though. It didn't seem to have any idea what process.env.NODE_ENV was.

Is it possible to access an environment variable like this in a template?


Solution

  • I ended up doing the following in my app.js

    nunjucks.configure('views', {
      ...
    }).addGlobal('NODE_ENV', process.env.NODE_ENV)
    

    This simply adds NODE_ENV as a globally accessible variable in my Nunjucks template.