简体   繁体   中英

Can't install Node.js plugin on Ubuntu 12.04

I have installed this database plugin for Node.js called 'db-mysql' and I've gone through all the steps mentioned on the site without getting errors, which must mean that the plugin is installed. But when I run my .js file (given below) in the Apache server I get this error.

Error: Cannot find module 'db-mysql'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/var/www/server.js:4:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)

Here is the sample code I'm running.

var http = require("http");
var mysql = require("db-mysql");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");

//Database Connectivity//
  new mysql.Database({
    hostname: 'localhost',
    user: 'root',
    password: '',
    database: 'test'
}).on('error', function(error) {
    console.log('ERROR: ' + error);
}).on('ready', function(server) {
    console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();
///////////////////////

  response.end();
}).listen(8889);

Did I miss anything during installation?

If you install module like in example

npm install db-mysql

Node will search it here

./project_root/node_modules/db-mysql

So make sure this path is correct in your case. Other option is to install it globally by using -g flag

npm install db-mysql -g

只需创建指向模块物理安装位置的sym链接将此链接命名为node_modules并将其放在项目的文件夹中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM