React - ssh2 module Error: Uncaught TypeError: __webpack_require__(...).constants is undefined

Viewed 269

I am trying to use https://github.com/theophilusx/ssh2-sftp-client in my React code. And I am getting Uncaught TypeError: __webpack_require__(...).constants is undefined at const Client = require('ssh2-sftp-client');

Code:

const StfpTransfer = props => {
    const filesToTransfer = props;
    
    const Client = require('ssh2-sftp-client');

    const config = {
      host: 'XX.XX.XX.XXXX',
      port: '22',
      username: '********',
      password: '********'
    };
    
    let sftp = new Client();
    
    sftp.connect(config)
      .then(() => {
        return sftp.list('/storage/share');
      })
      .then(data => {
        console.log(data);
      })
      .then(() => {
        sftp.end();
      })
      .catch(err => {
        console.error(err.message);
      });

}

export default StfpTransfer;

I am using the StfpTransfer from App.js, and in the code just first trying to test the sftp.

Error:

enter image description here

This is the zlib that errors https://github.com/mscdex/ssh2/blob/master/lib/protocol/zlib.js

I am wondering what I'm doing wrong here? Using pure javascript module or just some other basic mistake. I am a beginner with React/Nodejs so any advisory is a welcome one.

Edit, also here is the package.json if it's of any help:

{
  "name": "draganddrop",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "ssh2-sftp-client": "^7.0.0",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
1 Answers

Okay, the answer was quite simple.

Here I am running the ssh2-sftp-client in a browser side and it only works in server side.

So in order to solve this I needed to make server.js and run a server e.g. on port 8000 and use expressjs framework and then run the script here for example on a post command to the server.

Related