简体   繁体   English

React - ssh2 模块错误:未捕获的类型错误:__webpack_require__(...).constants 未定义

[英]React - ssh2 module Error: Uncaught TypeError: __webpack_require__(…).constants is undefined

I am trying to use https://github.com/theophilusx/ssh2-sftp-client in my React code.我正在尝试在我的 React 代码中使用https://github.com/theophilusx/ssh2-sftp-client And I am getting Uncaught TypeError: __webpack_require__(...).constants is undefined at const Client = require('ssh2-sftp-client');我得到了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.我正在使用StfpTransfer的 StfpTransfer,并且在代码中只是首先尝试测试 sftp。

Error:错误:

在此处输入图片说明

This is the zlib that errorshttps://github.com/mscdex/ssh2/blob/master/lib/protocol/zlib.js这是错误的 zlibhttps://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.使用纯 javascript 模块或其他一些基本错误。 I am a beginner with React/Nodejs so any advisory is a welcome one.我是 React/Nodejs 的初学者,所以任何建议都是受欢迎的。

Edit, also here is the package.json if it's of any help:编辑,如果有任何帮助,这里也是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"
    ]
  }
}

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.在这里,我在浏览器端运行ssh2-sftp-client ,它只在服务器端工作。

So in order to solve this I needed to make server.js and run a server eg on port 8000 and use expressjs framework and then run the script here for example on a post command to the server.所以为了解决这个问题,我需要制作server.js并在端口 8000 上运行一个服务器,并使用expressjs框架,然后在此处运行脚本,例如在服务器的 post 命令上。

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取 __webpack_require__ 处未定义的属性“调用” - Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ 未捕获的类型错误:__webpack_require__(...).context 不是 function - Uncaught TypeError: __webpack_require__(...).context is not a function 诊断未捕获的TypeError:__webpack_require __(…).createServer不是函数? - Diagnosing Uncaught TypeError: __webpack_require__(…).createServer is not a function? Karma Jasmine - 未捕获的类型错误:__webpack_require__(...).context 不是 function - Karma Jasmine - Uncaught TypeError: __webpack_require__(...).context is not a function TypeError: __webpack_require__(...).context 不是 function - 硬编码但不带参数 - TypeError: __webpack_require__(…).context is not a function - Works hardcoded but not with parameter __webpack_require__(...).context 不是 function - __webpack_require__(...).context is not a function Uncaught TypeError: WEBPACK_IMPORTED_MODULE_1__.default is undefined (React, SASS,SCSS) - Uncaught TypeError: WEBPACK_IMPORTED_MODULE_1__.default is undefined (React, SASS,SCSS) ReactJS 给出错误 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0__... 在实现上下文 API 时未定义 - ReactJS giving error Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0__... is undefined when implementing Context API 带有 ES6 和 webpack 的 AngularJS 1.7 给出错误:未捕获的类型错误:无法读取未定义的属性“模块” - AngularJS 1.7 with ES6 and webpack giving error: Uncaught TypeError: Cannot read property 'module' of undefined 使用webpack的react-redux给出错误Uncaught TypeError:超级表达式必须为null或函数,且未定义 - react-redux with webpack giving error Uncaught TypeError: Super expression must either be null or a function, not undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM