找不到模块`mysql` node.js

Str*_*ger 24 mysql node.js

我是nodejs的新手.要连接mysql,我mysql使用命令安装在节点上,

npm install mysql
Run Code Online (Sandbox Code Playgroud)

安装时我没有收到任何错误.然后我尝试执行以下代码,

var mysql = require("mysql");
Run Code Online (Sandbox Code Playgroud)

但是,当我试图执行它时,它显示以下错误.

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)
Run Code Online (Sandbox Code Playgroud)

我尝试了一些建议,比如全局安装mysql,

npm install -g mysql
Run Code Online (Sandbox Code Playgroud)

但没有任何作用.请帮忙!!!

请注意我的工作环境,

操作系统:Windows7节点版本:0.10.15 NPM版本:1.3.5

Bit*_*ive 17

我刚遇到同样的问题,发现这是因为模块安装在:

./node_modules/node-mysql/node_modules/

所以,我把它们全部移动了:

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


Mor*_*len 8

看起来您可能对npm install工作原理感到困惑。

npm install -g mysql 将在全球范围内安装,而不是像您建议的那样在本地安装。

npm install mysql将在本地安装,将模块放入./node_modules/mysql. 这意味着您正在运行的脚本需要从包含node_modules.


Seb*_*hke 7

你可以解决这个问题

ln -s /usr/local/lib/node_modules /YOURPROJECTFOLDER/node_modules
Run Code Online (Sandbox Code Playgroud)


cod*_*erz 7

node安装在C:\some-dir\nodejs-0.10.35\

首先导航到node安装的同一目录:cd C:\some-dir\nodejs-0.10.35

然后 npm install mysql

我将我的应用程序放在同一目录中: C:\some-dir\nodejs-0.10.35\applications\demo.js

有用.


小智 7

npm install mysql --save
Run Code Online (Sandbox Code Playgroud)

这将更新您的 package.json 文件。