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

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

我正在尝试在我的 React 代码中使用 https://github.com/theophilusx/ssh2-sftp-client。我在 const Client = require('ssh2-sftp-client');

得到 Uncaught TypeError: __webpack_require__(...).constants is undefined

代码:

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;

我正在使用 App.js 中的 StfpTransfer,并且在代码中首先尝试测试 sftp。

错误:

这是错误的 zlib https://github.com/mscdex/ssh2/blob/master/lib/protocol/zlib.js

我想知道我在这里做错了什么?使用纯 javascript 模块或其他一些基本错误。我是 React/Nodejs 的初学者,因此欢迎任何咨询。

编辑,还有package.json如果有帮助的话:

{
  "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"
    ]
  }
}

好吧,答案很简单。

我在这里 运行在浏览器端设置 ssh2-sftp-client,它只适用于服务器端。

所以为了解决这个问题,我需要制作 server.js 和 运行 一个服务器,例如在端口 8000 上,使用 expressjs 框架,然后 运行 此处的脚本,例如在对服务器的 post 命令上。