我正在尝试在我的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"
]
}
}发布于 2021-07-16 18:12:09
好吧,答案很简单。
在这里,我在浏览器端运行ssh2-sftp-client,它只在服务器端工作。
因此,为了解决这个问题,我需要创建server.js并运行服务器,例如在端口8000上,使用expressjs框架,然后在这里运行脚本,例如对服务器执行post命令。
https://stackoverflow.com/questions/68388377
复制相似问题