【问题标题】:Trouble getting Grunt to run node.js script让 Grunt 无法运行 node.js 脚本
【发布时间】:2015-10-31 19:46:59
【问题描述】:

我是一个 grunt 和节点菜鸟,但我设法编写了一个节点脚本,它可以执行我想要的操作并在命令行中工作。我不想将脚本发布为节点模块,但我想从我的 grunt 文件中运行它。

我需要对脚本进行哪些更改(如果有)才能使其正常工作?

我对配置 grunt 文件和自定义任务的了解越多,我就越困惑。我目前有这样的东西:

module.exports = function(grunt) {

    grunt.initConfig({
        'mytaskname': 'what goes here?'
    });

    grunt.loadNpmTasks('./node_modules/script_name');

    grunt.registerTask('run-from-command-line', 'description', function() {
        grunt.log.writeln('Not running...');
    });
}

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js gruntjs npm


    【解决方案1】:

    您可以使用grunt-execute 插件来执行此操作,它在 node.js 子进程中执行文件。

    示例: 如果您的节点脚本位于“node-scripts/script.js”中,则 Gruntfile.js 将如下所示:

    module.exports = function(grunt) {
        grunt.initConfig({
            execute: {
                target: {
                    src: ["node-scripts/script.js"]
                }
            }
        });
    
        // Load the plugins
        grunt.loadNpmTasks("grunt-execute");
    
        grunt.registerTask("default", ["execute"]);
    };
    

    【讨论】:

    • 我很感激。我通过制作自定义 grunt 任务找到了一种不同的方法,但我会将其标记为正确,因为它可能会在未来帮助另一个菜鸟。
    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 2013-10-15
    • 2021-09-27
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多