In my backend express server , I am trying to execute a github cli command using execSync. But it hungs up there. The command is following: `
execSsnc("gh auth login --web")
`
In the same machine, when I use terminal to execute the same command, it works fine. In fact, I have tried a executing this line of code in a simple nodejs project and this too works fine. But whenever I use this in my express server, it hungs up there. I have tried using spawn, exec, spawnSync too but no luck so far.
Also when I use execSync("gh auth status")
this gives error saying there is no github hosts currently. But in the machine, when I run this command from the terminal, it says that I am already logged it using Personal Access Token. Thus I found that running the login command from the express server is the main issue. Is there any way out?
CodePudding user response:
First, check if setting GH_DEBUG
to 1
or api
generates any verbose log which might shed some light to the issue.
const env = {
'env': {
'GH_DEBUG': 'api',
...process.env
}
}
exec('gh auth login --web', env, function (error, stdout, stderr){
console.log(stdout, stderr, error);
});
Second, check with another exec
who you are when executed from Express.js, and compare it to your command-line environment (where it does work)
Using gh auth login --token < aTokenFile
(passing the token as stdin to your execSync call) might be a better alternative than --web
.
CodePudding user response:
Note that execSync
create a new shell and executes gh
command in this shell.
Depending on the OS, the shell used by execSync
might be different than the shell used in your terminal (bash/sh) or miss some env variables that are available in your terminal. This might explain why "gh auth status"
execution shows different results.
Try to verify if the environment variables is your Express app are different than environment variables in your terminal