[英]Node can't find installed mysql module
I'm trying to use the node-mysql module to connect to my database. 我正在尝试使用node-mysql模块连接到我的数据库。 It was working fine, I updated my script (not even the connection script) and all in a sudden it can't locate the mysql module.
一切正常,我更新了我的脚本(甚至连连接脚本都没有),突然间它找不到mysql模块。
Here's my connection script, db_connect: 这是我的连接脚本db_connect:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'officeball'
});
and for reference, here are the two scripts that I changed, login.js: 作为参考,这是我更改的两个脚本,login.js:
console.log('login module initialized');
var express = require('express');
var app = express();
var validator = require('./validator');
var username;
var password;
function listen(){
app.use(express.bodyParser());
app.post('/login', function(req, res) {
console.log('User ' + req.body.email + ' is attempting login...');
username = req.body.email;
password = req.body.password;
validator.validate(username,password);
if (validator.validate() === req.body.email){
res.writeHead(302, {'Location': 'http://localhost/officeball/app.php'});
}
res.end();
});
app.listen(8080, function() {
console.log('Server running at http://127.0.0.1:8080/');
});
}
exports.listen = listen;
and the main change, validator.js: 和主要的变化,validator.js:
console.log('validator module initialized');
var login = require("./db_connect");
function validate(username, password, callback){
connection.connect(function (err){
console.log('Connection with the officeball MySQL database openned...');
if (err) return callback(new Error('Failed to connect'), null);
// if no error, you can do things now.
connection.query('select username,password from users where username=?',
username,
function(err,rows,fields) {
// we are done with the connection at this point), so can close it
connection.end();
console.log('...Connection with the officeball MySQL database closed.');
// here is where you process results
if (err)
return callback(new Error ('Error while performing query'), null);
if (rows.length !== 1)
return callback(new Error ('Failed to find exactly one user'), null);
// test the password you provided against the one in the DB.
// note this is terrible practice - you should not store in the
// passwords in the clear, obviously. You should store a hash,
// but this is trying to get you on the right general path
if (rows[0].password === password) {
// you would probably want a more useful callback result than
// just returning the username, but again - an example
return callback(null, rows[0].username);
} else {
return callback(new Error ('Bad Password'), null);
}
});
});
};
exports.validate = validate;
console log: 控制台日志:
C:\xampp\htdocs\officeball\node_scripts>npm install node-mysql
npm http GET https://registry.npmjs.org/node-mysql
npm http 200 https://registry.npmjs.org/node-mysql
npm http GET https://registry.npmjs.org/node-mysql/-/node-mysql-0.3.7.tgz
npm http 200 https://registry.npmjs.org/node-mysql/-/node-mysql-0.3.7.tgz
npm http GET https://registry.npmjs.org/cps
npm http GET https://registry.npmjs.org/better-js-class
npm http GET https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/mysql
npm http 200 https://registry.npmjs.org/better-js-class
npm http GET https://registry.npmjs.org/better-js-class/-/better-js-class-0.1.3.
tgz
npm http 200 https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz
npm http 200 https://registry.npmjs.org/cps
npm http GET https://registry.npmjs.org/cps/-/cps-1.0.0.tgz
npm http 200 https://registry.npmjs.org/better-js-class/-/better-js-class-0.1.3.
tgz
npm http 200 https://registry.npmjs.org/mysql
npm http 200 https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz
npm http 200 https://registry.npmjs.org/cps/-/cps-1.0.0.tgz
npm http GET https://registry.npmjs.org/require-all/0.0.3
npm http GET https://registry.npmjs.org/readable-stream
npm http GET https://registry.npmjs.org/bignumber.js/1.0.1
npm http 304 https://registry.npmjs.org/require-all/0.0.3
npm http 304 https://registry.npmjs.org/readable-stream
npm http 200 https://registry.npmjs.org/bignumber.js/1.0.1
npm http GET https://registry.npmjs.org/bignumber.js/-/bignumber.js-1.0.1.tgz
npm http 200 https://registry.npmjs.org/bignumber.js/-/bignumber.js-1.0.1.tgz
npm http GET https://registry.npmjs.org/debuglog/0.0.2
npm http GET https://registry.npmjs.org/core-util-is
npm http GET https://registry.npmjs.org/string_decoder
npm http 304 https://registry.npmjs.org/core-util-is
npm http 304 https://registry.npmjs.org/string_decoder
npm http 304 https://registry.npmjs.org/debuglog/0.0.2
node-mysql@0.3.7 node_modules\node-mysql
├── better-js-class@0.1.3
├── cps@1.0.0
├── underscore@1.6.0
└── mysql@2.1.0 (require-all@0.0.3, readable-stream@1.1.11, bignumber.js@1.0.1)
C:\xampp\htdocs\officeball\node_scripts>node index.js
application initialized
server module initialized
login module initialized
validator module initialized
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:\xampp\htdocs\officeball\node_scripts\custom_module
s\db_connect.js:1:80)
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 Module.require (module.js:364:17)
C:\xampp\htdocs\officeball\node_scripts>node index.js
application initialized
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:\xampp\htdocs\officeball\node_scripts\index.js:4:18
)
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)
C:\xampp\htdocs\officeball\node_scripts>node index.js
application initialized
server module initialized
login module initialized
validator module initialized
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:\xampp\htdocs\officeball\node_scripts\custom_module
s\db_connect.js:1:80)
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 Module.require (module.js:364:17)
C:\xampp\htdocs\officeball\node_scripts>
What newbie mistake have I made? 我犯了什么新手错误?
You want npm install mysql
, not npm install node-mysql
您要
npm install mysql
,而不要npm install node-mysql
The first one will install this module and the second one is installing this other one . 第一个将安装此模块 ,第二个将安装另一个 模块 。
Note that looking at the console output you will see than node-mysql
installs mysql
, but it is only as an internal dependency. 请注意,查看控制台输出后,您会看到
node-mysql
将安装mysql
,但这仅是内部依赖项。
Rule of thumb - your what
should be the same string 您-经验法则
what
应该是相同的字符串
Command line: 命令行:
npm install what
npm安装什么
Javascript code JavaScript代码
var what = require('what');
You are getting the npm package name confused (understandably, it's confusing in this case). 您将npm软件包名称弄糊涂了(可以理解,在这种情况下,这很令人困惑)。 The npm package name and the name you pass to
require
will always exactly match, BUT that doesn't mean the github repo will be the same name. npm软件包名称和您传递给
require
的名称将始终完全匹配,但这并不意味着github存储库将是相同的名称。 I think you want to do: npm install --save mysql
, which will give you the mysql
package, which happens to live in a github repo named node-mysql
. 我想您想做:
npm install --save mysql
,它将为您提供mysql
软件包,该软件包恰好位于名为node-mysql
的github存储库中。 By coincidence and annoyance, there is also a completely different npm package named node-mysql
(which violates conventions and civic sensibility, but anyway), which I doubt is the one you want. 碰巧和烦恼的是,还有一个完全不同的名为
node-mysql
npm软件包(它违反了约定和公民的敏感性,但无论如何),我怀疑这是您想要的软件包。
You should also do npm uninstall node-mysql
to clean up from your earlier mistake. 您还应该执行
npm uninstall node-mysql
来清除以前的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.