Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Npm install with success but nodejs cannot find module 'db'

Tags:

node.js

This error is so common but although I searched a lot of similar questions I did not found a solution to my problem. I installed the module with no problems using "npm install db".

The "server.js" is in the same folder as"node_moludes" and NPM created a "db" folder inside "node_moludes" with all the content.

All NPM commands return success (install, link, list etc), I even added the module to my "package.json" and it also installs with no problem just using "npm install -d".

Despite it all, when I run the server, Nodejs can't find the 'db' module... any help?

nodejs version: 0.10.26

npm version: 1.4.3

server.js

var http = require('http'),
    url = require('url'),
    db = require('db');

var send = function(response, data){
  response.writeHead(200, {
    'Content-Type': 'text/plain',
    'Access-Control-Allow-Origin': '*'
  });
  response.end(JSON.stringify({data: data}));
};

http.createServer(function (request, response) {
  var query = url.parse(request.url, true).query;
  if(query.set) {
    db.set(query.set, query.val || '');
    send(response, true);
  } else if (query.get) {
    db.get(query.get, function(data){
      send(response, data);
    });
  } else send(response, query);
}).listen(80);

package.json

{
  "name": "xxxxx",
  "version": "0.0.1",
  "author": "rafaelcastrocouto",
  "repository": "git://github.com/rafaelcastrocouto/xxxxx",
  "dependencies": {
    "db": ">= 1.00.x"
  },
  "engine": "node >= 0.10.x"
}
like image 339
rafaelcastrocouto Avatar asked Oct 29 '25 21:10

rafaelcastrocouto


1 Answers

It appears the db module was setup without following some Node.js norms. It doesn't contain an index.* file or specify a "main" setting in its package.json to direct Node otherwise.

You should be able to reference the main file within the package directly:

var db = require('db/db');
like image 63
Jonathan Lonowski Avatar answered Oct 31 '25 10:10

Jonathan Lonowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!