I'm using GitBash in windows 10 and want to execute git commands within a child_process.exec call. I think that since I installed git through "Git For Windows", I just need specify the shell as the GitBash executable. I've tried every variation of the path to GitBash executable that I could think of and it always fails. What is the path that node is looking for?
Example of paths that do not work
c:/program files/git/usr/bin/bash
c:/program\ files/git/usr/bin/bash
/c/program\ files/git/usr/bin/bash
c:\\program files\\git\\usr\\bin\\bash
const { expect } = require('chai');
const { exec } = require('child_process');
describe.only('exec', function(){
it('should work', function(done){
let shellPath = "c:\\program files\\git\\usr\\bin\\bash";
expect(exec(`cat <<< "abc"`, { shell: shellPath }, (err, stdout) => {
expect(err).to.be.null;
expect(stdout.trim()).to.be.equal("abc");
done();
}));
});
});
The first assertion fails with:
expected [Error: Command failed: cat <<< "abc" << was unexpected at this time.] to be null
There are some problems with this approach.
As the reference states, exec automatically uses Windows-specific shell arguments that won't work for Bash.
Another problem is that PATH may not be set to GitBash binaries path.
This should likely work:
delete process.platform;
process.platform = 'linux';
exec(`cat <<< "abc"`, {
env: { PATH: 'C:\\Program Files\\git\\usr\\bin' },
shell: 'C:\\Program Files\\git\\usr\\bin\\bash.exe'
}, (err, stdout) => {
...
});
process.platform = 'win32';
The workability of this solution may depend on bash.exe implementation.
The use of custom shell isn't needed to run git in Node; this is handled by Git executable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With