【问题标题】:How to use the Node debug module with Grunt?如何在 Grunt 中使用 Node 调试模块?
【发布时间】:2015-05-14 15:01:33
【问题描述】:

如何在 Grunt 中使用 Node 的 debug module

我知道要使用该模块,我需要在命令行中设置DEBUG env var,如下所示:

$ DEBUG=* node app.js

但是,我正在努力弄清楚如何使用 Grunt 做到这一点。

【问题讨论】:

  • 我不想在调试模式下运行节点。我想使用第三方调试模块。我只是不知道如何设置运行 Grunt 的 DEBUG 变量,而不是在命令行中设置它。
  • 所以你想在你的 grunt 代码中使用debug-module
  • 你的意思是var debug = require('debug')set DEBUG=* 在命令行...

标签: node.js gruntjs


【解决方案1】:

我使用grunt-env 帮助在 grunt 中设置环境变量,如下所示:

grunt.loadNpmTasks('grunt-env');

// Define the configuration for all the tasks
grunt.initConfig({

  // Env-specific configuration
  env: {
    options: {
      //Shared Options Hash
    },
    dev: {
      NODE_ENV: 'development',
      DEBUG: 'wukong:*,loopback:security:role,-not_this'
    },
    test: {
      NODE_ENV: 'test',
      DEBUG: 'wukong:*,-not_this'
    }
  },
  // ...
}

但是,它不起作用,因为调试模块在模块初始化的一开始就加载并保存process.env.DEBUGhttps://github.com/visionmedia/debug/blob/master/node.js#L209

因此,我玩了一个技巧,通过其“隐藏”公共 API - enable 再次加载 DEBUG 变量:

const debug = require('debug');
debug.enable(process.env.DEBUG);
module.exports = debug;

当使用此 debug 包装器时,grunt 可以按预期打印出调试消息。

希望此解决方法也适用于您。 ;)

【讨论】:

    猜你喜欢
    • 2014-08-23
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多