[英]I'm trying to link Node.js+Webpack+MySQL, but it's stuck in fs module. Help me
I tried to run the program by linking MySQL and Node.js, but fs cannot be loaded due to the following errors.我尝试通过链接MySQL和Node.js来运行程序,但是由于以下错误无法加载fs。
Module not found: Error: Can't resolve 'fs'找不到模块:错误:无法解析“fs”
By the way, adding target:'node' , causes a global is not defined error.顺便说一句,添加target:'node'会导致全局未定义错误。
and add node {fs:'empty'} , causes a createConnection is not a function error.并添加节点 {fs:'empty'} ,导致createConnection is not a function错误。
What should I do now我现在该怎么办
webpack.config.js
webpack.config.js
const path = require('path');
module.exports = (env, argv) => {
return {
mode: 'production',
entry: {
index : path.join(__dirname, 'src' ,'index.ts'),
},
output: {
path: path.join(__dirname, 'www'),
filename: 'game.js',
library: 'game',
libraryTarget: 'umd'
},
resolve: {
extensions: ['.ts', '.js'],
modules: [
path.resolve(__dirname, 'src'),"node_modules"
]
},
module: {
rules: [
{
test : /\.ts$/,
use: [{ loader: 'ts-loader'}]
}
]
}
}
};
mysql function
mysql function
function get_connection():void {
console.log("mysql : ",mysql)
console.log("mysql.createConnection", mysql.createConnection)
connection = mysql.createConnection({
host: "host",
user: "user",
password: "pass",
database: "database"
}).then(conn => {
console.log('promise-mysql createConnection.');
connection = conn;
return conn;
}).catch(err => {
console.log("promise fail:",err)
})
}
As you're keen to use webpack to bundle for node server code, you shouldn't bundle node_nodules by using this helper webpack-node-externals
.由于您热衷于使用 webpack 来捆绑节点服务器代码,因此不应使用此帮助
webpack-node-externals
来捆绑 node_nodules 。 So try more with these options:因此,请尝试使用以下选项进行更多尝试:
var nodeExternals = require('webpack-node-externals')
target: 'node',
externals: [
nodeExternals(),
],
node: {
fs: "empty",
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.