Thursday, October 20, 2016

MySQL Connection using NodeJS and How to resolve Error: Cannot find module 'mysql'

var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
host : 'localhost', 
user : 'siddhu', 
password : 'siddhu', 
database : 'vscrum' 
}); 
connection.connect(); 
connection.query('SELECT * FROM vscrum.kpi', function(err, results) 

if (err) 

console.error(err); 

else 

console.log('First row of department table : ', results[0]); 

}); 
connection.end();
Note: How to resolve Error: Cannot find module 'mysql'
Above statement says that your project did not get mysql module to execute.
If you are working on window or linux resolutions is same. As i am using Window and my project TestSiddhuNodeJs is in C:\workspace-nodejs\ use follwing below command to execute
Step 1:- Go to C:\workspace-nodejs\TestSiddhuNodeJs
Step 2:- Execute npm install mysq
C:\workspace-nodejs\TestSiddhuNodeJs
`-- mysql@2.11.1
+-- bignumber.js@2.3.0
+-- readable-stream@1.1.14
| +-- core-util-is@1.0.2
| +-- inherits@2.0.3
| +-- isarray@0.0.1
| `-- string_decoder@0.10.31
`-- sqlstring@2.0.1
npm WARN enoent ENOENT: no such file or directory, open 'C:\workspace-nodejs\TestSiddhuNodeJs\package.json'
npm WARN TestSiddhuNodeJs No description
npm WARN TestSiddhuNodeJs No repository field.
npm WARN TestSiddhuNodeJs No README data
npm WARN TestSiddhuNodeJs No license field.
Step 3:- Execute your programe and see the result.

No comments: