14

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

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

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

connection.end();

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 connection.js:

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

The connection.js 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

9
  • This might be helpful Commented Jun 24, 2016 at 5:15
  • As you've debugged it and found an underlying cause it is possible that there might be a problem with the node.js version. Which node version are you using exactly? Commented Jun 24, 2016 at 5:25
  • My node is version 4.2.3 Commented Jun 24, 2016 at 5:39
  • could it be that your Amazon credentials? somewhere? not sure though Commented Jun 24, 2016 at 7:06
  • its not the amazon credentials. The error indicates that the net.connection method cannot be found Commented Jun 26, 2016 at 23:11

1 Answer 1

29
+150

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 object and the error is related to one of your comment to the question:

I am testing my code within a 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 into 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 in 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.

Sign up to request clarification or add additional context in comments.

2 Comments

but this wouldn't happen if i'm running a localserver with express, wouldn't it? because i'm facing the same error and can't find any solution to this problem. but it only appears, if i'm using createConnection, if i'm using pooling, it works brilliantly.
what if I use rest API to create connection and saved data? I hope that will work with Webpack and browserify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.