简体   繁体   中英

NPM nodemon and debug no output in terminal

I have this simple code:

const express = require('express');
const chalk = require('chalk');
const debug = require('debug');
const morgan = require('morgan');
const path = require('path');

const app = express();
const port = process.env.PORT || 3000;

app.use(morgan('tiny'));
app.use(express.static(path.join(__dirname, '/public')));
app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist')));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '/views/index.html'));
});

app.listen(port, () => {
    debug(`listening on port ${chalk.green(port)}`);
});

The problem is that whatever I do, the output for starting the app listening on port 4000 does not appear, but the rest is ok, everything works as it should. It should 100% work and maybe you can help me. Thanks!

Per the documentation and your app here is a trimmed down example of your app, modified, and the output.

documentation: https://www.npmjs.com/package/debug

Your app (trimmed down and modified slightly) - note the addition of the something . This is what debug attaches to

const express = require('express');
const chalk = require('chalk');
const debug = require('debug')('something');

const app = express();
const port = process.env.PORT || 3004;

debug('some debug statement');

app.listen(port, () => {
    debug(`listening on port ${chalk.green(port)}`);
});

The call from a terminal line:

$ DEBUG=something node ~/path_to_your_project/project_name/app.js

The output:

Chriss-MBP-3:untitled1 lcsharp$ DEBUG=something node

~/WebstormProjects/untitled1/app.js
  something some debug statement +0ms
  something listening on port 3004 +9ms

If you want to use the debug module then you have to initialize the logger with a namespace:

const debug = require('debug')('my-namespace')

By default the output of debug is not logged at all, you need to use the DEBUG environment variable to define what should be logged. How depends on the shell you use, and could look like this:

DEBUG=* node index.js

DEBUG=* means that everything is logged, as * is a wildcard.

If you would like to only log my-namespace it has to be:

DEBUG=my-namespace node index.js

You can divide your loggin into individal parts like:

const debugApp = require('debug')('my-namespace:app')
const debugModuleA = require('debug')('my-namespace:module-a')
const debugModuleB = require('debug')('my-namespace:module-b')

And with my-namespace:* you can log app , module-a and module-b

The same can be done for nodemon :

DEBUG=my-namespace nodemon index.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM