Search code examples
javascriptnode.jsssh2

Node ssh2: TypeError: <Object> is not a constructor


I have node v14.17.0 and ssh2 1.1.0 https://www.npmjs.com/package/ssh2

I have tried to get the connection working with code below but it crashes on TypeError: NodeSSH is not a constructor

I have also tried

var NodeSSH= require('ssh2');

var c = new NodeSSH();

And

var NodeSSH= require('ssh2').Client;

And

const {NodeSSH} = require('ssh2');
const c = new NodeSSH();

c.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
    console.log('Connection :: keyboard-interactive');
    finish(['pswd']);
    }).on('end', function() {
        console.log('Connection :: end');
        console.log(callback());
    }).on('error', function(error) {
        console.log(error);
    }).connect({
        host: 'XX.XX.XXX.XXX',
        username: 'usr',
        port: "22",
        tryKeyboard: true,
        debug: console.log
    });

I can't seem to figure out what is causing this.


Solution

  • I think you should do something like this, the way you are getting the instance is incorrect.

    
    const { Client } = require('ssh2'); // it exports Client not NodeSSH
    
    const conn = new Client();
    
    conn.on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
        console.log('Connection :: keyboard-interactive');
        finish(['pswd']);
        }).on('end', function() {
            console.log('Connection :: end');
            console.log(callback());
        }).on('error', function(error) {
            console.log(error);
        }).connect({
            host: 'XX.XX.XXX.XXX',
            username: 'usr',
            port: "22",
            tryKeyboard: true,
            debug: console.log
        });
    
    

    You should check the documentation how to use it though you have already shared it in the question. I suggest you going through it once.