mysql: Cannot resolve modules fs, net and tls

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();

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 10
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Fixed this by adding

node: {
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
  },

to webpack config, but Idk why it does that yet…

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

Are you doing client or server webpack? If server this might help http://jlongster.com/Backend-Apps-with-Webpack--Part-I