Require('child_process')=>找不到Require/child_process +启动.bat [英] Require('child_process') => cannot find Require / child_process + launch .bat

查看:75
本文介绍了Require('child_process')=>找不到Require/child_process +启动.bat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经3天了,我正在论坛上搜索一种使程序生效的方法,但仍然无效……

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

我正在做我的第一个AngularJS项目,当用户单击图像时,我需要启动外部程序.

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

我有一个名为tool-list的模块,在我的tool-list.component.ts中,我导出了一个名为ToolListComponent的类.在此课程中,我有启动我的外部程序的功能.

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!");
    }

在评论中,我尝试了几行.

我的问题:当我使用 require => 错误TS2304找不到名称"require"时.或者只是 child_process => 错误找不到模块'child_process'.

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

我绝对不是第一个遇到这种问题的人,但是没有一个解决方案对我有用(import,import * as,require,声明函数require(name:string),var require:any; [...]).

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.

服务器和客户端将位于客户端,我只是将Web用作HMI

推荐答案

除非您的应用程序在诸如

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...

也就是说,如果您希望批处理文件在服务器上运行 并仅由客户端触发,那是完全可行的-只需调用 child_process.spawn()响应某种REST调用.

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.

这篇关于Require('child_process')=>找不到Require/child_process +启动.bat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆