Access Node Environment Variables in Jade file Access Node Environment Variables in Jade file express express

Access Node Environment Variables in Jade file


Anything you want to access in the jade template has to be in the locals object sent down from the server. For something like the process environment, you can do this right when you fire up your app:

const express = require('express');var app = express();app.locals.env = process.env;  // though you might prefer to clone this instead of setting them equal

Then in your jade template you can do

#{env.NODE_ENV}

UPDATE

Adding for direct use, rather than in an express server.

const pug = require('pug');// Compile the source codeconst compiledFunction = pug.compileFile('template.pug');// Render a set of dataconsole.log(compiledFunction(process.env));

That'll log it, but of course you could just as easily write that to an HTML file using fs utilities instead.