user1526912 user1526912 - 3 months ago 14x
SQL Question

Error in MySQL library for Node.js

In my Node.js app, I am trying to connect to a MySQL database hosted on Amazon.

$ npm install mysql

My code looks something like this:

var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'my amazon sql db',
user : 'me',
password : 'secret',
database : 'my_db'


connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;

console.log('The solution is: ', rows[0].solution);


I can connect to my MySQL DB using Workbench--therefore, I am pretty sure my credentials are okay.

When I attempt to connect I get the following error:

Connection.js:91 Uncaught TypeError: Net.createConnection is not a function

Debugging the code from the npm library--this is where the error is thrown in

this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port,;

has a dependency :

var Net = require('net');

I am running Node.js locally on my Windows computer.

Can anyone tell me what could be causing this error?

Created a separate ticket:
Error thrown calling Node.js net.createConnection


The net module required and used in the MySQL node module is a core part of Node.js itself. The error you're getting about Net.createConnection not being a function means it's coming up as an empty objet and the error is related to one of your comment to the question:

I am testing my code within browser.

You must run this particular module on Node.js only, you can't run it in a web browser.

One could think a possibility would be to run your code through a packer like browserify or webpack so you can easily require('mysql') in your browser but it won't work. The net module which is a core dependency of the mysql module will be transformed to an empty object {}. That's not a bug, it's how it's supposed to work. Browsers don't have generic tcp implementations so it can't be emulated. The empty object is intended to prevent require('net') from failing on modules that otherwise work in the browser.

To avoid this error, you need to run this code on a pure Node.js environment, not in a browser. A simple server could serve this purpose since this code in your client in a browser can't work and would add a security hole as everything client-side is manipulative and as such not secure. You don't want to expose your database on the client-side but only consumes it.