38

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

6
  • 2
    Are you running your script from the same directory you ran npm install mysql? Commented Aug 8, 2013 at 19:27
  • Also, using npm install -g is global install. Commented Aug 8, 2013 at 19:27
  • @MorganARRAllen How to run it on the same directory? Commented Aug 8, 2013 at 19:34
  • 1
    Try running npm ls from that same directory that app.js is in. This will list out the npm modules that are installed for that project. Commented Aug 8, 2013 at 20:02
  • @JaimeMorales It is showing [email protected] ── [email protected] ── [email protected] under the tree Commented Aug 8, 2013 at 20:45

16 Answers 16

19

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/

Sign up to request clarification or add additional context in comments.

2 Comments

I installed it more than one time using npm install -g mysql but I get :/usr/local/lib/node_modules/mysql# ls node_modules bignumber.js readable-stream require-all, I don't find node-mysql, please advise about the path to do the above, because it doesn't work
I use the mysql2 2.2.5 version which does not have cardinal but the helper.js requires for the cardinal as below. How to solve this? Error: Can't walk dependency graph: Cannot find module 'cardinal' from '\node_modules\mysql2\lib' required by node_modules\mysql2\lib\helpers.js
16
npm install mysql --save

This will update your package.json file.

Comments

12

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.

1 Comment

I use the mysql2 2.2.5 version which does not have cardinal but the helper.js requires for the cardinal as below. How to solve this? Error: Can't walk dependency graph: Cannot find module 'cardinal' from '\node_modules\mysql2\lib' required by node_modules\mysql2\lib\helpers.js
10

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.

Comments

7

You can fix this with

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

2 Comments

for me, this one really get past the error "Cannot find module 'mysql'"
Actually, the other way around ln -s /YOURPROJECTFOLDER/node_modules /usr/local/lib/node_modules worked for me (Debian 10)
3

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.

Comments

2

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

npm install mysql --save

1 Comment

This exact same answer was added a full 5 months before you added this one.
1

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

Comments

1
npm install mysql2 --save

and require this one

var mysql = require("mysql2");

Comments

0

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.

Comments

0

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.

Comments

0

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

require('mysql')

to

require('mysql2')

and it worked.

Comments

0

This worked for me

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

Comments

0

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

Comments

0

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

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

Comments

-1

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

1 Comment

npm install mysql --save will add this dependency for you without you having to edit the file manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.