Javascript Node.JS exec git命令错误:权限被拒绝(公钥)

Javascript Node.JS exec git命令错误:权限被拒绝(公钥),javascript,node.js,git,ssh,Javascript,Node.js,Git,Ssh,问题:如何使用passphrase从node.js运行gitpush 我正在尝试构建一个小模块,在这个模块中,我需要从node.js运行git push到一个远程repo,但是当我使用fromnode.js exec而不是从终端运行时,我遇到了一个错误 我的密码 /command.ts import * as util from 'util'; const exec = util.promisify(require('child_process').exec); export default

问题:如何使用
passphrase
node.js
运行
gitpush

我正在尝试构建一个小模块,在这个模块中,我需要从
node.js
运行
git push
到一个远程
repo
,但是当我使用from
node.js exec
而不是从终端运行时,我遇到了一个错误

我的密码

/command.ts

import * as util from 'util';

const exec = util.promisify(require('child_process').exec);

export default function command(command: string): Promise<string> {
  return exec(command, {cwd: process.cwd()}).then((resp) => {
    const data = resp.stdout.toString().replace(/[\n\r]/g, '');
    return Promise.resolve(data);
  });
}
import command from './command';

async function init() {
 try {
  await command('git add .');
  await command('git commit -m "my commit" ');
  conat result = await command('git push');
 } catch (e) {
  console.log(e);
 }
}

init();
当我运行
ts node./index.ts
时,我得到以下错误

Error: Command failed: git push                                                                                                         
git@hostname.org: Permission denied (publickey).                                                                                       
fatal: Could not read from remote repository.                                                                                           

Please make sure you have the correct access rights                                                                                     
and the repository exists.
但是,当我从终端运行
gitpush
时,我会得到密码短语提示,它可以工作

关于如何解决这个问题,有没有办法使用
node.js
使用密码运行
gitpush

请记住,我很乐意在没有任何外部LIB的情况下修复此问题

提前感谢。

同样,在以下情况下检查同一程序是否有效:

  • ssh代理已启动
  • 你的

不仅提示不再查询您的密码短语(现在由代理缓存),而且您的脚本也可能从该缓存中受益。

如果密钥加载到ssh代理进程中,您可能需要将
env:process.env
添加到
exec()
选项中。ssh-agent导出了一些环境变量,其他进程使用这些变量查找ssh-agent以访问其密钥。

如果您可以使用用户名和密码,您可以轻松地执行git-pushhttps://username:password@myrepository.biz/file.git--所有感谢您的回复,但是,如果没有ssh密钥,则会提示用户用户名和密码pass@diegoddox嗯,是的。。。但在您的情况下,有一个ssh密钥,对吗?我有,但我将被交给模块的人可能没有,最好是
exec
可以在需要时提示
passphrase/user:pass
。@diegodox那么https URL可能更好:他们可以缓存自己的凭据。