Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in MySQL library for Node.js

Tags:

node.js

mysql

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

like image 562
user1526912 Avatar asked Jun 24 '16 05:06

user1526912


People also ask

Is MySQL compatible with node js?

Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver.

How install MySQL connect node JS?

Quick Start: How to Use MySQL in Node Create a new project: mkdir mysql-test && cd mysql-test . Create a package. json file: npm init -y . Install the mysql module: npm install mysql .

What is Node JS error?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


1 Answers

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.

like image 151
HiDeo Avatar answered Oct 12 '22 12:10

HiDeo