简体   繁体   中英

execute sh file using node.js on windows OS

I'm working on node.js application. I'm trying to execute the .sh file from node.js file (on windows operating system) .. but it is throwing error that 'sh' is not recognized as an internal or external command,operable program or batch file.

File hi.sh

echo "Hi There!"

File app.js

var exec = require('child_process').exec;
var myscript = exec('sh ~/hi.sh');
myscript.stdout.on('data',function(data){
    console.log(data); 
});
myscript.stderr.on('data',function(data){
    console.log(data); 
});

Windows does not support the Bash scripting language out of the box.

With Windows 10 you can install WSL (Windows Subsystem for Linux), wich gives you Bash support.

After you installed WSL you should be able to use exec('bash ~/hi.sh'); (not tested), but using it from PowerShell works.

PS> bash -c "bash --version"
GNU bash, Version 4.3.11(1)-release (x86_64-pc-linux-gnu)[...]

On Windows, you can use batch scripts ( .bat files). So you could have hi.bat :

echo "Hi There!"

and then presumably run this with something like:

var myscript = exec('cmd hi.bat');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM