Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module mysql - error

I have installed nodejs and mysql(also the work bench)

I am building a server using nodejs and mysql.

in my code I write:

var mysql = require('mysql');
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
  user: 'root',
  password: 'root',
});

client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
  if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
    throw err;
  }
});

and the compiler gives me an error:

Error: cannot find module 'mysql'

like image 255
user690936 Avatar asked Jan 15 '12 20:01

user690936


3 Answers

This happens when you don't have the module installed, so go to the root of your project and install node-mysql:

npm install mysql

You don't need to manually copy the folder yourself, dependencies are best handled with NPM.

like image 127
alessioalex Avatar answered Oct 12 '22 23:10

alessioalex


i think that i solved it.

there is a folder node_modules under the nodejs folder in that folder there is a mysql folder. copied it to the folder that from there i am running my program, and i t works:)

like image 35
user690936 Avatar answered Oct 12 '22 23:10

user690936


  1. Goto your project folder.... in my case command prompt command
  2. type as show in screenshot

npm install mysql

  1. After installation you will see the following directories. enter image description here
like image 28
Adiii Avatar answered Oct 13 '22 00:10

Adiii