If I try to connect to a database Webpack throws this:
ERROR in ./~/mysql/lib/Connection.js
Module not found: Error: Cannot resolve module 'net' in C:\Wamp64\www\react\node_modules\mysql\lib
@ ./~/mysql/lib/Connection.js 2:23-37
ERROR in ./~/mysql/lib/Connection.js
Module not found: Error: Cannot resolve module 'tls' in C:\Wamp64\www\react\node_modules\mysql\lib
@ ./~/mysql/lib/Connection.js 3:23-37
ERROR in ./~/mysql/lib/protocol/sequences/Query.js
Module not found: Error: Cannot resolve module 'fs' in C:\Wamp64\www\react\node_modules\mysql\lib\protocol\sequences
@ ./~/mysql/lib/protocol/sequences/Query.js 6:19-32
I don't know if this is an error my mysql or if it is caused by webpack or nodejs (I cannot load the module even if I write var x = require("fs"); outside the mysql function (anywhere in my code))
My webpack.config.js:
module.exports = {
entry: "./src/App.js",
output: {
path: __dirname,
filename: "app.js"
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel",
query: {
presets: ["es2015", "react"]
}
}]
}
};
And my package.json:
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.7.*",
"babel-loader": "^6.2.*",
"babel-preset-es2015": "^6.18.*",
"babel-preset-react": "^6.16.*",
"webpack": "^1.13.*",
"webpack-dev-server": "^1.16.*"
},
"dependencies": {
"react": "^15.3.*",
"react-dom": "^15.3.*",
"mysql": "^2.12.*",
"express": "^4.14.*"
}
}
My Code (just the mysql function):
var mysql = require("mysql");
var user = this.refs.username.value;
var password = this.refs.password.value;
var sqlCommand = "SELECT * FROM users WHERE user = '".concat(username, "' AND pass = '", password, "'");
//console.log(user + " - " + password);
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "lg"
});
connection.connect();
connection.query(sqlCommand, function(err, rows, fields){
if(!err){
console.log(rows);
console.log(fields);
}else{
console.log("Error while querying: ".concat(err));
}
});
connection.end();
I'm not sure if this module can be used with webpack? I'm not very familiar, and have just never used webpack. Any hints / PRs you can make would help out a lot :) !
i'm having the exact same issue. Also using webpack. I've tried older versions of the package and it's still the same problem. Where are the 'net', 'tls' and 'fs' modules supposed to be? Cause they dont seem to be in my node_modules/mysql folder.
Any info you need to help sort this out, i'm here.
Here's a pastebin of my console logs http://pastebin.com/M7AcvVEX.
It appears it's searching for the modules on the node_modules and the mysql/node_modules folders, and the files are not there.
Are you doing client or server webpack? If server this might help http://jlongster.com/Backend-Apps-with-Webpack--Part-I
Where are the 'net', 'tls' and 'fs' modules supposed to be?
They are built directly into Node.js itself.
The true solution to this seems to be to add target: 'node' to your module.exports object in webpack.config.js. At least, that was the solution for me.
if i add target: 'node' to module.exports it return another bug: ReferenceError: require is not defined
Fixed this by adding
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
},
to webpack config, but Idk why it does that yet...
Where are the 'net', 'tls' and 'fs' modules supposed to be?
They are built directly into Node.js itself.
@dougwilson as someone who's new to webpack and node, what does your answer imply? I'm still not sure how we are supposed to get the build to work if we want to use mysql in a frontend script.
I've never worked on the frontend and I just don't have an answer. If someone can help provide an answer, I'd be happy to relay.
ok gotcha sorry i thought you closed this issue bc there was a known solution.
I tried @tanohzana's solution but then when my script loads, I'm unable to call any of mysql's functions (createConnection, query, etc..).
I tried target: node but then I ran into ReferenceError: require is not defined. I wasn't expecting that to work though since my target is not node.
I mainly close this because this issue gets opened periodically and I have not seen any solution that works. Without a path forward, the issue is just noise. If someone can provide what the path forward is, I can always reopen this or any of the other issues for this. As of now, we don't support what you're trying to do since we don't know how to support it. Just Node.js is supported until someone contributes a web browser solution.
I'm working on something unrelated to this mysql project but came across this same issue when attempting to use nodemailer. It looks like it may be a bug in webpack: https://github.com/webpack-contrib/css-loader/issues/447
I'm still facing the same issue.
I'm adding it to an Ionic project and when attempting to run it, I've the following error:
[ng] ERROR in ./node_modules/mysql/lib/Connection.js
[ng] Module not found: Error: Can't resolve 'crypto' in '/Users/flavio/quizAdmin/node_modules/mysql/lib'
[ng] ERROR in ./node_modules/mysql/lib/protocol/Auth.js
[ng] Module not found: Error: Can't resolve 'crypto' in '/Users/flavio/quizAdmin/node_modules/mysql/lib/protocol'
[ng] ERROR in ./node_modules/mysql/lib/protocol/sequences/Query.js
[ng] Module not found: Error: Can't resolve 'fs' in '/Users/flavio/quizAdmin/node_modules/mysql/lib/protocol/sequences'
[ng] ERROR in ./node_modules/mysql/lib/Connection.js
[ng] Module not found: Error: Can't resolve 'net' in '/Users/flavio/quizAdmin/node_modules/mysql/lib'
[ng] ERROR in ./node_modules/mysql/lib/protocol/Protocol.js
[ng] Module not found: Error: Can't resolve 'stream' in '/Users/flavio/quizAdmin/node_modules/mysql/lib/protocol'
[ng] ERROR in ./node_modules/mysql/lib/protocol/Timer.js
[ng] Module not found: Error: Can't resolve 'timers' in '/Users/flavio/quizAdmin/node_modules/mysql/lib/protocol'
[ng] ERROR in ./node_modules/mysql/lib/Connection.js
[ng] Module not found: Error: Can't resolve 'tls' in '/Users/flavio/quizAdmin/node_modules/mysql/lib'
Any idea how to fix it?
This is not an mysqljs-issue.
These errors occur mainly due to webpack not being configured properly for backend-usage.
After superficially reading some "webpack and node.js" stuff via google and stackoverflow,
I suggest putting mysqljs in the externals section of the backend-config.
Most helpful comment
Fixed this by adding
to webpack config, but Idk why it does that yet...