【问题标题】:React - ssh2 module Error: Uncaught TypeError: __webpack_require__(...).constants is undefinedReact - ssh2 模块错误:未捕获类型错误:__webpack_require__(...).constants 未定义
【发布时间】:2021-09-24 00:58:51
【问题描述】:

我正在尝试在我的 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。

错误:

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

我想知道我在这里做错了什么?使用纯 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"
    ]
  }
}

【问题讨论】:

    标签: javascript node.js reactjs node-modules


    【解决方案1】:

    好的,答案很简单。

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2021-05-13
      • 2021-08-11
      相关资源
      最近更新 更多