How To Execute Windows Shell Commands (Cmd.exe) with Node JS

最后都变了- 提交于 2019-12-18 18:54:03

问题


I would like to

C:\>ACommandThatGetsData > save.txt

But instead of parsing and saving the data in the console, I would like to do the above command with Node.JS

How to execute a shell command with Node.JS?


回答1:


You could also try the node-cmd package:

const nodeCmd = require('node-cmd');
nodeCmd.get('dir', (err, data, stderr) => console.log(data));



回答2:


Use process.execPath():

process.execPath('/path/to/executable');

Update

I should have read the documentations better.

There is a Child Process Module which allows to execute a child process. You will need either child_process.exec, child_process.execFile or child_process.spawn. All of these are similar in use, but each has its own advantages. Which of them to use depends on your needs.



来源:https://stackoverflow.com/questions/15919347/how-to-execute-windows-shell-commands-cmd-exe-with-node-js

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