ERROR in ./node_modules/cpu-features/build/Release/cpufeatures.node 1:2
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./node_modules/cpu-features/lib/index.js 3:16-60
@ ./node_modules/ssh2/lib/protocol/constants.js 7:12-35
@ ./node_modules/ssh2/lib/server.js 26:4-38
@ ./node_modules/ssh2/lib/index.js 33:10-32
@ ./src/app.js 3:19-34
ERROR in ./node_modules/ssh2/lib/protocol/crypto/build/Release/sshcrypto.node 1:2
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./node_modules/ssh2/lib/protocol/crypto.js 30:12-60
@ ./node_modules/ssh2/lib/server.js 27:29-60
@ ./node_modules/ssh2/lib/index.js 33:10-32
@ ./src/app.js 3:19-34
如何解决?我在 Webpack.org 中找不到合适的加载器
webpack.config.js
const path = require("path");
module.exports = {
resolve: {
fallback: {
fs: false,
tls: false,
net: false,
path: false,
zlib: false,
http: false,
https: false,
stream: false,
crypto: false,
buffer: false,
util: false,
assert: false,
dns: false,
process: false,
timers: false,
url: false,
child_process: false,
string_decoder: false,
},
},
entry: "./src/app.js",
mode: "production",
output: {
filename: "app.js",
path: path.resolve(__dirname, "dist"),
},
};
app.js
const conf = require("./config.js");
const mysql = require("mysql2");
const { Client } = require("ssh2");
// create an instance of SSH Client
const sshClient = new Client();
const SSHConnection = new Promise((resolve, reject) => {
sshClient
.on("ready", () => {
sshClient.forwardOut(
conf.forwardConfig.srcHost,
conf.forwardConfig.srcPort,
conf.forwardConfig.dstHost,
conf.forwardConfig.dstPort,
(err, stream) => {
if (err) reject(err);
// create a new DB server object including stream
const updatedDbServer = {
...dbServer,
stream,
};
// connect to mysql
const connection = mysql.createConnection(updatedDbServer);
// check for successful connection
// resolve or reject the Promise accordingly
connection.connect((error) => {
if (error) {
reject(error);
}
resolve(connection);
});
}
);
})
.connect(conf.tunnelConfig);
});
SSHConnection.then(
(resolve) => {
console.log("ssh resolved");
},
(reject) => {
console.log(`ssh reject: ${reject}`);
}
);
package.json
{
"type": "commonjs",
"private": true,
"scripts": {
"build": "webpack",
"start": "node ./dist/app.js"
},
"devDependencies": {
"dotenv": "^16.0.3",
"webpack": "^5.77.0",
"webpack-cli": "^5.0.1"
},
"dependencies": {
"mysql2": "^3.2.0",
"ssh2": "^1.11.0"
}
}
要处理 Node.js 附加组件,例如
ssh2
,您需要使用适当的加载器。因为它可以在 Webpack 页面 上找到,所以您可以像下面这样使用node-loader
:
module.exports = {
entry: "./src/app.js",
mode: "production",
output: {
filename: "app.js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
},
],
},
};
别忘了安装它:
npm install --save-dev node-loader