'ssh-keygen npm package gives undefined public and private keys

Output for the keys is undefined for both private and public key [ssh-keygen][1]

Keys created!

private key: undefined
public key: undefined

async generateAndWriteSSHKeyv2() {
return new Promise((resolve, reject) => {

    let t = new Date().getTime();
    var location = path.join(process.cwd(), `contents/apps/SSHkeys/key_${t}`);
    var comment = 'test';
    var password = 'test';
    var format = 'PEM';
    keygen({
        location: location,
        comment: comment,
        password: password,
        read: true,
        destroy: false,
        format: format,
        size: 4096,
    }, (err, output) => {
        if (err) resolve(console.log('Something went wrong: ' + err));
        console.log('Keys created!');
        console.log('private key: ' + output.key);
        console.log('public key: ' + output.pubKey);
        resolve({
            location,
            comment,
            password,
            read: true,
            format,
            size: 4096,
            output
        })
    });
})
};


Solution 1:[1]

I have found this example, for you to compare with your own code

  generateSshKeyFiles(name, next) {
    keygen({
      location: name,
      read: true,
      destroy: true,
    }, function(err, out) {
      if (err) {
        next(err);
      } else {
        let sshKeyFiles = [{'content': out.pubKey, 'fileName': name+'.pub'}, {'content': out.key, 'fileName': name}];
        next(null, sshKeyFiles);
      }
    });
  }

Check if you get the same kind of error message with that type of function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC