代码之家  ›  专栏  ›  技术社区  ›  Node.JS

Node.js exec生成Marp标记不起作用

  •  0
  • Node.JS  · 技术社区  · 4 年前

    console.log("test") exec 也不会运行Marp build命令。

    如果我从终端运行“cmd”字符串,它就会正常工作。我认为我的使用方式有问题

    var glob = require("glob");
    var cp = require("child_process");
    
    glob("lab*/*.marp.md", {}, function (err, files) {
      files.forEach((file) => {
        var destination = file.replace(".md", "");
        var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
        var dir = cp.exec(cmd, (err, stdout, stderr) => {
          if (err) {
            console.log("node couldn't execute the command");
            return;
          }
    
          // the *entire* stdout and stderr (buffered)
          console.log(`stdout: ${stdout}`);
          console.log(`stderr: ${stderr}`);
        });
    
        dir.on("exit", function (code) {
          // exit code is code
          console.log(`finished building: ${file}`);
        });
      });
    });
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Robiul    4 年前

    您可以尝试execSync()函数,它将解决此问题

    var glob = require("glob");
    var cp = require("child_process");
    
    glob("lab*/*.marp.md", {}, function (err, files) {
      files.forEach((file) => {
        var destination = file.replace(".md", "");
        var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
    
        var dir = cp.execSync(cmd, (err, stdout, stderr) => {
          if (err) {
            console.log("node couldn't execute the command");
            return;
          }
    
          // the *entire* stdout and stderr (buffered)
          console.log(`stdout: ${stdout}`);
          console.log(`stderr: ${stderr}`);
        });
    
        dir.on("exit", function (code) {
          // exit code is code
          console.log(`finished building: ${file}`);
        });
      });
    });
    
    推荐文章