[英]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.