找不到模块`mysql` node.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18134411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:29:17  来源:igfitidea点击:

Cannot find module `mysql` node.js

mysqlnode.js

提问by Stranger

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

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

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,

我尝试了一些建议,例如使用全局安装 mysql,

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

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

采纳答案by Bitwise Creative

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

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

./node_modules/node-mysql/node_modules/

./node_modules/node-mysql/node_modules/

So, I just moved them all:

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

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

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

回答by coderz

My nodeis installed in C:\some-dir\nodejs-0.10.35\

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

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

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

Then npm install mysql

然后 npm install mysql

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

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

It works.

有用。

回答by Sebastian Weschke

You can fix this with

你可以用

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

回答by Morgan ARR Allen

It looks like you might be confused about how npm installworks.

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

npm install -g mysqlwill install globally not locally like you suggest.

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

npm install mysqlwill 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.

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

回答by user991802

npm install mysql --save

This will update your package.json file.

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

回答by joelc

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

我遇到了同样的问题(当然我在 Windows 8 上)。我试着npm install mysqlnpm install -g mysql和既不工作。

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

结果我需要打开“Node.js 命令提示符”应用程序,而不是常规的命令提示符应用程序。一切都很好。

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.

我不知道他们的命令提示符在幕后做了什么,但我认为它与路径和环境变量有关。您可能想尝试一下。

回答by abhishek bv

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

您可能需要更新 package.json 文件。使用以下命令

npm install mysql --save

回答by hoogw

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.

将解决问题。有时由于某种原因,mysql 包被损坏、删除或丢失,只需运行上面将修复一切。

回答by Salman lodi

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

我注意到npm install -g mysql在 node_modules 中保存了 mysql2。刚换了

require('mysql')

to

require('mysql2')

and it worked.

它奏效了。

回答by Atul Kumar

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
  1. 首先创建一个 proj 目录,例如 (Users/home/proj)

  2. 转到然后项目文件夹。

  3. 运行npm init(这将创建 packaje.json)。
  4. 跑 ( npm install mysql)
  5. 检查 proj 文件夹,你应该看到 node_modules 文件夹,展开 node_modules 你应该看到 mysql 文件夹。
  6. 运行你的 app.js