0

I am having this issue since more than one month and I read all related posts on stackoverflow and other sites but none of them worked out with me! The MySQL module of Node.js is not getting recognized by Node.js

I have installed Node.js as below:

apt-get install nodejs

Then installed the npm as below:

apt-get install npm

Then installed the MySQL module as below:

npm install -g mysql

I also tried the below without any success too!

npm install mysql

When I run the Node.js server like below:

node /var/www/server.js

I get:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: Cannot find module 'mysql'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at Object.<anonymous> (/var/www/server.js:2:13)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:32)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)

The content of server.js as below

console.log("Hello World");
var mysql = require('mysql');

Update: When I used

cd /var/www/

then

npm install mysql

The MySQL doesn't work, I use the below to connect to MySQL

var db_config = {
    host      : 'localhost',
    user      : 'xxx',
    password  : 'xxx',
    database  : 'xxx'
};

var connection;

function handleDisconnect(){

    connection = mysql.createConnection(db_config);


    connection.connect(function(err) {
        if(err) {
            console.log('error when connecting to db:', err);
            setTimeout(handleDisconnect, 2000);
        }
    });

    connection.on('error', function(err) {
        console.log('db error', err);
        if(err.code === 'PROTOCOL_CONNECTION_LOST') {
            handleDisconnect();
        }
        else{
             throw err;
        }
    });
 }

 handleDisconnect();   

I am not into advance settings of Node.js and I like to work on simple commands.

I would greatly appreciate your advise because I face this issue whenever I install Node.js on the server. Thanks.

moderns
  • 650
  • 10
  • 23
  • Do you have a directory called `node_modules` in the same place you have `server.js`? Is there a directory called `mysql` in the `node_modules` directory? – vch Aug 24 '14 at 14:34
  • No, node_modules directory is not in the directory of server.js. Before 1 month I installed it this way and it worked but I don't know why now it is not working. I can see the directory node_modules in the root as below node_modules/mysql/ – moderns Aug 24 '14 at 14:38
  • use npm without the `g` (global) flag to install your modules there. usually, node modules are installed on a project-by-project basis. – vch Aug 24 '14 at 14:40
  • Thanks but I tried "npm install mysql", and didn't work. Please hint me what command to run? Shall I install the modules inside the same directory where the server exists? – moderns Aug 24 '14 at 14:46

1 Answers1

1

Try the following:

cd /var/www/
npm install mysql

This way the mysql module will be installed locally on your node directory.

My guess is that you have tried to install the module from another directory, not from where your server.js located.

David
  • 2,528
  • 1
  • 23
  • 29
  • Thanks a lot but this way it is not connecting to the database. I have updated my post. I used to connect to DB normally before. – moderns Aug 24 '14 at 15:50
  • Did you get any error? Just did copy/paste to my local, seems working. – David Aug 25 '14 at 00:36
  • I get general error: Error: connect ECONNREFUSED. I don't know why! Any hints please? Seems something with the library or something need to be added? – moderns Aug 25 '14 at 11:23
  • It may be the port, check out this one: http://stackoverflow.com/questions/21206801/node-js-mysql-error-econnrefused – David Aug 25 '14 at 13:57