Stranger
Stranger

Reputation: 10631

Cannot find module `mysql` node.js

i am a newbie to nodejs. To connect mysql, i installed mysql on node using the command,

npm install mysql

I didn't get any error while installing. Then i tried executing the following code,

var mysql = require("mysql");

But it is showing the following error while im trying to execute that.

C:\node\mysql>node app.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'mysql'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\node\mysql\app.js:1:75)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

I tried some suggestion like installing mysql globally using,

npm install -g mysql

But nothing works. Help please!!!

Please note my working environment,

OS: Windows7 Node version: 0.10.15 NPM version: 1.3.5

Upvotes: 38

Views: 130533

Answers (16)

Feng Zhang
Feng Zhang

Reputation: 1960

if using package.json, simply add below and run "npm install" in your project.

{ "dependencies": { "mysql": "2.12.0" } }

Upvotes: 0

Rohit Soni
Rohit Soni

Reputation: 55

npm install MySQL --save

This is because you have not install MySQL in your package. You are importing this without installing MySQL in your system environment

Upvotes: 0

Miniyahil Kebede
Miniyahil Kebede

Reputation: 21

npm install mysql2 --save

and require this one

var mysql = require("mysql2");

Upvotes: 1

Atul Kumar
Atul Kumar

Reputation: 81

I faced the same issue. Here is how solved it.

  1. First created a proj dir e.g (Users/home/proj)

  2. Go to then proj folder.

  3. Run npm init (this will create packaje.json).
  4. Run (npm install mysql)
  5. Check in proj folder, you should see node_modules folder, expand node_modules and you should see mysql folder.
  6. Run your app.js

Upvotes: 1

user991802
user991802

Reputation: 361

npm install mysql --save

This will update your package.json file.

Upvotes: 16

Arjon Arts
Arjon Arts

Reputation: 39

This worked for me

  • go to your project folder
  • run the following command: npm install mysql

Upvotes: 0

Salman lodi
Salman lodi

Reputation: 26

I noticed that on npm install -g mysql has saved mysql2 in node_modules. Just changed

require('mysql')

to

require('mysql2')

and it worked.

Upvotes: 0

hoogw
hoogw

Reputation: 5535

I have same error, just run

npm install

will fix the problem. Sometime due to some reason, mysql package was damaged, or deleted, or missing, just run above will fix everything.

Upvotes: 0

abhishek bv
abhishek bv

Reputation: 81

You might have to update the package.json file. Use the following command

npm install mysql --save

Upvotes: 2

jwood
jwood

Reputation: 373

I have found this happens if you run npm install without having dependencies defined in your package.json ... i.e.

...
"author": "Author",
"dependencies" : {
     "mysql": "2.12.0",    
},
"license": "ISC"
...

Define the dependecies .. then run

npm install

Upvotes: -1

harshvardhan
harshvardhan

Reputation: 805

Navigate to folder /node_modules which is inside the main directory, and install mysql by hitting the following command: sudo npm install mysql

This will create a folder called mysql inside the /node_modules folder.

Now run your app using node app.js command inside the main folder. It shall work and establish the connection with mysal server.

Upvotes: 0

coderz
coderz

Reputation: 4999

My node is installed in C:\some-dir\nodejs-0.10.35\

First navigate to the same directory node installed: cd C:\some-dir\nodejs-0.10.35

Then npm install mysql

I put my applications in the same directory: C:\some-dir\nodejs-0.10.35\applications\demo.js

It works.

Upvotes: 12

Sebastian Weschke
Sebastian Weschke

Reputation: 71

You can fix this with

ln -s /usr/local/lib/node_modules /YOURPROJECTFOLDER/node_modules

Upvotes: 7

joelc
joelc

Reputation: 2761

I was having the same issue (granted I am on Windows 8). I tried npm install mysql and npm install -g mysql and neither worked.

Turned out I needed to open the 'Node.js Command Prompt' app rather than the regular command prompt app. Everything worked great.

I don't know what their command prompt does under the hood but I would assume it has something to do with pathing and environment variables. You may want to give it a try.

Upvotes: 3

Bitwise Creative
Bitwise Creative

Reputation: 4105

I just ran into the same problem and found it was because the module was installed in:

./node_modules/node-mysql/node_modules/

So, I just moved them all:

mv ./node_modules/node-mysql/node_modules/* ./node_modules/

Upvotes: 19

Morgan ARR Allen
Morgan ARR Allen

Reputation: 10678

It looks like you might be confused about how npm install works.

npm install -g mysql will install globally not locally like you suggest.

npm install mysql will install locally, putting the module in ./node_modules/mysql. This means the script you are running needs to be run from the same directory containing node_modules.

Upvotes: 10

Related Questions