【问题标题】:NPM nodemon and debug no output in terminalNPM nodemon和调试终端没有输出
【发布时间】:2019-03-11 19:24:40
【问题描述】:

我有这个简单的代码:

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)}`);
});

问题是,无论我做什么,启动应用程序listening on port 4000 的输出都不会出现,但其余的都可以,一切正常。它应该 100% 工作,也许你可以帮助我。谢谢!

【问题讨论】:

    标签: javascript node.js terminal output


    【解决方案1】:

    根据文档和您的应用,这里是您的应用、修改和输出的精简示例。

    文档:https://www.npmjs.com/package/debug

    您的应用程序(精简并稍作修改)- 注意添加了something。这就是调试所附加的

    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)}`);
    });
    

    来自终端线路的呼叫:

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

    输出:

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

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

    【讨论】:

    • 问题是关于debug 模块而不是关于console.debug
    • 谢谢。现在修改我的答案。
    【解决方案2】:

    如果您想使用debug 模块,那么您必须使用命名空间初始化记录器:

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

    默认情况下debug 的输出根本不记录,您需要使用DEBUG 环境变量来定义应该记录的内容。具体如何取决于您使用的外壳,可能如下所示:

    DEBUG=* node index.js
    

    DEBUG=* 表示记录所有内容,因为* 是通配符。

    如果您只想记录my-namespace,则必须是:

    DEBUG=my-namespace node index.js
    

    您可以将您的登录信息分成几个部分,例如:

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

    使用my-namespace:*,您可以登录app、module-a 和module-b

    nodemon 也可以这样做:

    DEBUG=my-namespace nodemon index.js
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 2021-05-21
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多