Reputation: 385
I am developing application in Node.js on OS X and I am using a lot of libraries (AWS, express, redis, Sequelize with pgSQL...). I have recently added new module that is using leveldb (I have installed it through 'level' package). Module looks like:
var levelup = require('level');
function init(config) {
return levelup(config.level_db, {keyEncoding: 'json', valueEncoding: 'json'});
}
module.exports = {
init: init
};
If I require it in main 'server.js' file, everything works. But when I want to assign result of calling 'init' to some variable, running the code returns:
192:qubie garretraziel$ node server.js
undefined:0
TypeError: undefined is not a function
192:qubie garretraziel$
I am pretty sure that this isn't some kind of syntax error, because the very same code runs without problem on my Linux machine. If I delete some random 'require('library');' (with appropriate code) it fails again but later (it starts listening on port, but then fails):
192:qubie garretraziel$ node server.js
App is running on port 8080
undefined:0
TypeError: undefined is not a function
192:qubie garretraziel$
and when I delete enough 'require's, code works. If I 'extract' the code from module and do 'var db = levelup(config...)' in server.js directly instead, it runs without problem. I have tried to reinstall all modules, reinstall Node.js and install different version using NVM, but nothing helps. Is there some kind of limit on how much memory can module use or anything like that? Strange thing is that on my Linux machine it works without problem (and uses from 50 to 100 MB of RAM).
Edit: My server.js file looks like:
var http = require('http');
var https = require('https');
var express = require('express');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var winston = require('winston');
var configuration = require('./configuration/configuration');
var setup = require('./lib/setup');
var bc11m = require('./lib/bc11n');
var memdb = require('./lib/memdb');
var drawdb = require('./lib/drawdb');
var models = require('./models');
var app = express();
var config = configuration[app.get('env')];
var db = models(config);
var memstore = memdb.init(config);
var drawstore = drawdb.init(config);
var server;
function runServer() {
var sessionStore = new RedisStore({client: memstore});
setup.settings(app, config, db, sessionStore);
setup.routes(app, config, db, memstore);
if (config.ssl) {
server = https.createServer(config.options, app);
} else {
server = http.createServer(app);
}
bc11m.init(server, config, db, memstore, sessionStore, drawstore);
server.listen(config.port, function () {
console.log("App is running on port " + config.port);
});
}
db.sequelize.sync().complete(function (err) {
if (err) {
winston.error('during syncing database: %s', String(err))
} else {
runServer();
}
});
And that levelup module is called drawdb.js. This code fails, but when I do:
var drawstore = levelup(config...
It works. Or when I delete 'require('./lib/bc11n')' and 'require('./models')' and few others requires, it also works.
Upvotes: 0
Views: 179
Reputation: 106698
According to this issue, someone else using OS X ran into the same problem. Having level
use the latest version of leveldown
(1.0.0 as of this writing) seems to fix the problem. Evidently there are some compatibility issues preventing level
from updating to a newer version of leveldown
, so you may be out of luck for now on OS X.
Upvotes: 1