简体   繁体   English

节点 ssh2:类型错误:<Object> 不是构造函数

[英]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我有节点v14.17.0和 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我试图让连接使用下面的代码,但它在TypeError: NodeSSH is not a constructor崩溃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.我似乎无法弄清楚是什么导致了这种情况。

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.我建议你通过它一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM