node.jsexpressnodeenv

Show logs with condition on process.env.NODE_ENV


I try to show logs on my ExpressJS app only on the dev process environement.

In package.json : set NODE_ENV=dev & node index.js

My condition :

server.app.use(function(request, response, next) {
  console.log(process.env.NODE_ENV, typeof process.env.NODE_ENV);
  if (process.env.NODE_ENV === 'dev') {
    console.log(colors.green('URL : ' + request.originalUrl));
  }
  next();
});

I'm not passing the condition but as you can see above, I'm on the dev environment.

enter image description here


Solution

  • To avoid trailing spaces for environment variables, the parameter can be set in brackets.

    i.e.

    (set NODE_ENV=dev) & node index.js