node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

When used on the renderer process of the Electron environment, the message "Class constructor NodeSSH cannot be invoked without 'new'" is displayed and instantiation fail.

Open TakamiChie opened this issue 3 years ago • 4 comments

Hello.

I am considering the use of NodeSSH in the Electron environment. I made the NodeSSH module available on the renderer process using a preload mechanism in order to reflect the processing result on the screen.

// main.js
const { app, BrowserWindow } = require('electron')
const path = require("path");

let win
function createWindow() {
  win = new BrowserWindow({
    width: 400,
    height: 400,
    webPreferences: {
      nodeIntegration: true, 
      preload: path.join(__dirname, './src/preload.js'),
      contextIsolation: true,
    }
  });
  // ...
}
app.on('ready', () => {
  createWindow();
})
// preload.js
const { contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld(
  "requires", {
    nodessh : require('node-ssh'),
    ipcRenderer : ipcRenderer,
  }
);
// index.js(In RendererProcess)
const {NodeSSH} = window.requires.nodessh;
const ssh = new NodeSSH();
// ...

In this way, electron execution itself is possible, but on the second line of the index .js, an error of "Class constructor NodeSSH cannot be invoked without 'new'" occurs and the script stops executing.

When I checked in electron's developer console, the value of "window.requires.nodessh" seems to be "{SSHError: ƒ, NodeSSH: ƒ}", and I think the loading itself is successful, but is there any missing description?


Environment

Windows 11 Pro Version 21H2 Build: 22000.613

npm -v

8.4.1

.\node_modules.bin\electron -v

v18.1.0

TakamiChie avatar Apr 29 '22 13:04 TakamiChie