首页 > 解决方案 > 在 React 中使用 node-cmd... 获取 TypeError: exec is not a function

问题描述

我在我的反应应用程序中使用 npm node-cmd 并且在 cmd.get 上失败(向终端发送命令)。

我所有的代码都在 vanilla html 和 javascript 中工作,但我需要它在 React 中工作。我认为我的设置反应错误。

     import cmd from 'node-cmd';


     let projDir = result.substr(0, result.lastIndexOf('/')+1);
     let projFile = result.substr(result.lastIndexOf('/')+1);

     let copyFile = "cp '" + projFile + "' pproXML.gz";
     let unzip = "gunzip -d pproXML.gz";
     let rename = "mv pproXML pproXML.prproj";
     let targetXml = projDir + 'pproXML.prproj';  

     let cmdStr =
          "cd ..'" + projDir + "'\n" +
          copyFile + "\n" +
          unzip + "\n" +
          rename + "\n" +
          "ls";

     // FAILS HERE
     cmd.get(
          cmdStr,
          function(err, data, stderr){
               alert(data);
          }

我收到 TypeError: exec is not a function

标签: javascriptnode.jsreactjsnpm

解决方案


exec is not a function告诉我事情出错了node-cmd

只需打开库: var exec = require('child_process').exec;

该行将永远无法在浏览器中使用。试一试ls


推荐阅读