0

I'm trying to take input using readline but am running into an unknown error after doing so. To make matters worse, my friend has no difficulties running the exact same piece of code on his device.

const { Octokit } = require("@octokit/rest");
const readline = require("readline");
const util = require('util');

const octokit = new Octokit();
const rl = readline.createInterface(process.stdin, process.stdout);
const question = util.promisify(rl.question).bind(rl);

const getUsername = async () => { return await question("Enter GitHub username: "); }
const getRepoData = async (uname) => { return await octokit.request("/users/" + uname +  "/repos"); }


(async () => {
    try {
        console.log("checkpoint")
        const uname = await getUsername();
        console.log("checkpoint2")
        console.log(uname);
        var repoData = await getRepoData(uname); 
        repoData = repoData["data"]; // is a list of dictionaries, e.g. repoData[0]["name"]
        console.log(repoData);
    } catch (err) {
        console.log("FAILED");
        console.log(err);
    } finally {
        rl.close();
    }
})() 

My intention is that the program prints out information about a given GitHub user. However, running this program in console with node app.js results in

checkpoint
Enter GitHub username: fuzzyhappy
FAILED
fuzzyhappy

I have no idea why it's printing back the response to the question either.

Evan Wang
  • 1
  • 1
  • The script is obviously running the `catch` block. You have to remove the code in the `try` block line by line until you find the line producing the error. See [this question](https://stackoverflow.com/questions/38597908/how-do-i-debug-node-js-errors-when-my-code-is-nowhere-in-the-stack-trace) for possible ways to debug your app. Note that without additional info about the error, nobody can help: *"I have an unknown problem. Could you please solve it for me?"* – tao Jan 22 '22 at 20:29
  • Oh! I should have mentioned that the code runs fine without the first two lines in the `try` block. If I hardcode `uname` to a known username, it correctly retrieves information from GitHub. Sorry I left out that information! – Evan Wang Jan 22 '22 at 20:33
  • In that case, the problem likely lies with `util.promisify`. Figure out what's different on your machine compared to the one that runs fine. e.g: are they running different node versions? Possibly [related question](https://stackoverflow.com/questions/45064380/function-works-but-fails-when-util-promisify-used). In more detail, would `util.promisify(rl.question.bind(rl));` work? – tao Jan 22 '22 at 21:03
  • 1
    It was a Node version difference... Thank you! – Evan Wang Jan 22 '22 at 21:33

0 Answers0