Require('child_process') => cannot find Require / child_process + launch .bat

北战南征 提交于 2020-01-30 10:58:11

问题


It has been 3 days I'm searching on forums a way to make work my program, and it still doesn’t work…

I'm working on my first AngularJS project, and I need to launch an external program when the user clicks on an image.

I have a module named tool-list, and in my tool-list.component.ts, I export a class named ToolListComponent. In this class I have my function to launch my external program.

    launchtool(){
      alert("I'm trying to launch test.bat!");
      const spawn = require('child_process').spawn;
      //const bat = spawn('start', ['./TrashCode/test.bat']);
      //const bat = spawn('cmd.exe', ['start', './TrashCode/test.bat']);
      //var exec = child_process.spawn('start', './TrashCode/test.bat', {detached: true});
      alert("It works!");
    }

In commented I keep a few line I tried.

My problem: When I use require => error TS2304 cannot find name 'require'. Or just child_process => error cannot find module 'child_process'.

I'm definitly not the first who has this kind of problem, but neither of the solution worked for me (import, import * as, require, declare function require(name:string), var require: any; [...]).

Maybe the solution is evident for some of you; I'm reviewing my lessons in this case.

Server and Client will be on the client side, Im just using web as an HMI


回答1:


You can't run a child process through a web browser, unless your app is running in a desktop app container such as Electron (which makes Node modules available within its web runtime). It would be horrendously insecure to allow browsers to execute shell scripts - imagine if a pop-up was able to download a .bat and immediately run it on your machine...

That said, if you want the batch file to run on the server and just be triggered by the client, that's perfectly feasible - just call child_process.spawn() in response to a REST call of some kind.



来源:https://stackoverflow.com/questions/36721793/requirechild-process-cannot-find-require-child-process-launch-bat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!