可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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. (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
回答1:
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/
回答2:
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.
回答3:
You can fix this with ln -s /usr/local/lib/node_modules /YOURPROJECTFOLDER/node_modules
回答4:
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.
回答5:
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.
回答6:
You might have to update the package.json file. Use the following command npm install mysql --save
回答7:
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.
回答8:
This solution from coderz worked fine.
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
Thank you.
回答9:
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
回答10:
npm install mysql --save
This will update your package.json file.