1

I am trying to use npm install debug in my node server.

var debugModule = require('debug');
var debugMainApp = debugModule('debugMainApp')

const express = require('express');

const app =  express();

const port ='3000';
const domain = 'localhost';

app.listen(port,domain,()=>{
     debugMainApp('Server started in port: ' + port + ' hostname: ' + domain);
});

debugMainApp('Server started in port: ' + port + ' hostname: ' + domain); is not printing anything to console.

My attempts to solve this issue
By setting the attribute debugMain.enabled = true manually debugMainApp('Server started in port: ' + port + ' hostname: ' + domain); printed the following into console:

mainApp Server started in port: 3000 hostname: localhost +0ms

To my understanding this debugMain.enabled attribute should be set automatically when an environment variable that matches the string debugModule('this_String') is set.

Here is how I set the environment variable and start my server

$ DEBUG=debugMainApp & node server.js

But it seems that the latter is not setting the environment variable properly.

Questions

  1. If you think my environment variable theory is right. What is the correct way to set an environment variable using a GitBash command line? e.g.$ DEBUG=mainApp & node server.js
  2. Could it be something else?

Thanks in advance.

More relevant info:
My OS: Windows 10
GitBash Version: 2.13.0.windows.1
NodeJS version: 6.10.3

RESOLUTION enter image description here

Jorge González
  • 2,898
  • 1
  • 9
  • 12
  • 1
    Can you try `DEBUG=debugMain node server.js` – VonC Feb 19 '18 at 20:32
  • _when an environment variable that matches the string debugModule('this_String') is set._ Yes, this string would be `mainApp` but you're calling it with `debugMain`. – Vasan Feb 19 '18 at 20:33
  • It was a typo on my part, these strings DO match on my code. The '&' in $ DEBUG=debugMain & node server.js was messing things up. – Jorge González Feb 19 '18 at 20:43

1 Answers1

2

The & would indicate to the bash to launch a process in background.

The right syntax is:

DEBUG=mainApp node server.js

See more at "Why is setting a variable before a command legal in bash?"

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250