Use node.js to determine if in git directory

You can try to redirect stdout inside the execSync call, like this:

var test = execSync('git rev-parse --is-inside-work-tree 2>/dev/null', {encoding: 'utf8'});

If you use docker and don't want to install git as a system-level dependency when build image for your application. Maybe because we want to build the image faster and keep the image size as small as possible.

The way @janos provides will not work.

Another way is to check is there a .git directory or not in root path of your project. But it depends on your requirements. For my case, it's enough.

exports.isGitSync = function isGitSync (dir) {
  return fs.existsSync(path.join(dir, '.git'))
}