Error in module export with Node.js - javascript

I have an issue with exporting a module in Node.js.
Here is a part of my code:
var zmq = require('zmq');
var module = require('module');
var DeviceRequester = function(port, name)
{
...
};
var SMValueGetter = function(socket)
{
...
};
module.exports.DeviceRequester = DeviceRequester;
module.exports.SMValueGetter = SMValueGetter;
When I use it I get the following error:
module.exports.DeviceRequester = DeviceRequester;
^
TypeError: Cannot set property 'DeviceRequester' of undefined
at Object.<anonymous> (<PROJECT_PATH>/node_modules/lse/lib/lse.js:168:32)
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)
at require (module.js:380:17)
at Object.<anonymous> (<PROJECT_PATH>/server.js:6:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
I have no idea why it occurs. I've red the Node.js documentation and some tutorials about exporting modules and all of them do it that way. Am I overlooking something?

Give more attentions at errors
TypeError: Cannot set property 'DeviceRequester' of undefined
at line var module=require("module"); you are overwriting the default module variable.
simple try to change at something that makes more sense, such as
var MyModule=require(..path..)

Related

Failure when initializing PouchDB

Here is my stack trace when attempting to launch my api side of my react - web app.
I have ran into this error multiple times but never seem to be able to fix it when it comes up again!
/Users/joshua_paulsen/Desktop/Jump-In-Final/api/node_modules/pouchdb-core/lib/index.js:1285
throw new Error('Invalid Adapter: ' + opts.adapter);
^
Error: Invalid Adapter: undefined
at new PouchDB (/Users/joshua_paulsen/Desktop/Jump-In-Final/api/node_modules/pouchdb-core/lib/index.js:1285:11)
at Object.<anonymous> (/Users/joshua_paulsen/Desktop/Jump-In-Final/api/dal.js:18:12)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/Users/joshua_paulsen/Desktop/Jump-In-Final/api/routes/workouts/index.js:8:5)
I had my db defined like this - const db = new PouchDB(
${process.env.COUCH_HOSTNAME}${process.env.COUCH_DBNAME}
)
the fix ended up being changing it to this.
var pouch = new PouchDB(${process.env.COUCH_DBNAME}, { adapter: "http" })

What is this syntax error ? (Testing Hapi.js)

I'm just trying to test Hapi.js and, whatever I code, I got this error :
node_modules/hapi/lib/server.js:145
Items.serial(this.connections, (connectionItem, next) => {
^
SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:448:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/gilles/exo1/node_modules/hapi/lib/index.js:5:16)
at Module._compile (module.js:430:26)
at Object.Module._extensions..js (module.js:448:10)
Where does come form this syntax ...() => {... to make a function ?
What I did bad to use Hapi ?
Thank you very much
The syntax is called Arrow function. You use hapi 10 or later which requires node 4+, but you have an older version of node which doesn't support arrow functions yet. The solution is to upgrade your node version to 4 or 5.

Unexpected token ILLEGAL while running node.js mocha test

I have searched all over but have not figured out what I am doing wrong. I am trying to set up mocha for testing node.js javascript application files. I have node installed and have successfully ran basic things on it to confirm it is working.
I installed mocha in my project file, and also have a Makefile and a file called "test" within my project file as well.
Here is the error my terminal(osx 10) is spitting out when I run the command "make test".
humbleMousesMBP:chapter02 humbleMouse$ make test
/Users/humbleMouse/chapter02/test/exchange.test.js:22

^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
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)
at require (module.js:380:17)
at /Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:313:27
at Array.forEach (native)
at load (/Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:310:9)
at Object.<anonymous> (/Users/humbleMouse/chapter02/node_modules/mocha /bin/_mocha:301:1)
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)
at startup (node.js:119:16)
at node.js:906:3
make: *** [test] Error 8
This is the test I am trying to run:
'use strict';
var assert = require('assert')
, should = require('should');
var exchangeData = {};
suite('exchange', function() {
test('buy should add a BUY nockmarket order', function(done) {
exchangeData = exchange.buy(40, 100, exchangeData);
exchangeData.buys.volumes[40].should.eql(100);
done();
});
test('sell should add a SELL nockmarket order', function(done) {
exchangeData = exchange.sell(41, 200, exchangeData);
exchangeData.sells.volumes['41'].should.eql(200); //this is line 22
done();
});

test('sell should produce trades', function(done) {
exchangeData = exchange.sell(40, 75, exchangeData);
exchangeData.trades[0].price.should.eql(40);
exchangeData.trades[0].volume.should.eql(75);
exchangeData.buys.volumes[40].should.eql(25);
exchangeData.sells.volumes[41].should.eql(200);
done();
});
});
There are some invalid characters in your code, if you used proper text editor, you'd see them. The line numbering is a bit off, but this is clearly the cause.
Here's a screenshot from Sublime Text:
It's \uFFFC, more info here.
Just delete them (they can't be seen, so delete all from the semicolon to the next test().

Node.js "Cannot read property 'prototype' of undefined" error - Node.js, MongoDB and Angularjs book

I am working through Brad Dayley's Node.js, MongoDB and Angularjs book and I'm stuck on one of his exercises (Listing 4.4). I have a simple script emitterListener.js that is as follows the script is designed to makes checks on an account.
var events = require('events');
function Account() {
this.balance = 0;
events.EventEmitter.call(this);
this.deposit = function(amount) {
this.balance += amount;
this.emit('balanceChanged');
};
this.withdraw = function(amount) {
this.balance -= amount;
this.emit('balanceChanged');
};
}
Account.prototype._proto_ = events.EventEmitter.prototype;
function displayBalance() {
console.log("Account balance: $%d", this.balance);
}
function checkOverdraw() {
if (this.balance < 0) {
console.log("Account Overdrawn!!!!!");
}
}
function checkGoal(acc, goal) {
if (acc.balance > goal) {
console.log("Goal Achieved!!!!!");
}
}
var account = new Account();
account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function() {
checkGoal(this, 1000);
});
account.deposit(220);
account.deposit(320);
account.deposit(600);
account.withdraw(1200);
When I run this I get the error.
TypeError: Object #<Account> has no method 'on'
at Object.<anonymous> (/Users/506132/web/emitterListener.js:35:9)
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)
at startup (node.js:119:16)
at node.js:906:3
From my limited understanding after researching I think this means that the "on" module is not being loaded. I found a solution that suggested something similar to adding this to line 1
var events = require('events').on;
which then results in the error.
TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
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)
at startup (node.js:119:16)
at node.js:906:3
Following the logic from the first fix I tried implementing the same fix but with EventEmitter
var events = require('events').EventEmitter;
Hooray it looks like it worked.... or not......and now I get this error
TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:17:48)
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)
at startup (node.js:119:16)
at node.js:906:3
I tried adding the below code thinking why not?
var events = require('events').prototype;
and it just brings me back to the same error from before
TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
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)
at startup (node.js:119:16)
at node.js:906:3
What am I doing wrong here? How should I go about debugging this and where should I look? Thanks in advance for helping a newbie out.
Cheers.
process.EventEmitter is deprecated, use require('events') instead.
Search through all your reference files (should be in multiple files) for:
var EventEmitter = process.EventEmitter
An where ever it exists, change that line to this:
var EventEmitter = require('events')
I'll post it as an answer so this question won't get marked as unanswered.
You should change:
Account.prototype.__proto__ = events.EventEmitter.prototype;
to:
Account.prototype = Object.create(events.EventEmitter.prototype);
var EventEmitter = process.EventEmitter
is deprecated in high version of node, use var EventEmitter = require('events') instead
This compilation error because of versions conflicts of npm and node. I had the same issue in my project. I resolved after install the same compatible versions of npm and node as on another machine.
Check:
nmp -v
and
node -v.
All the confusion with the actual example is the first line of code.
var events = require('events');
After Node v6.1.0 this line of code will store EventEmmiter object inside events variable, so there is no need to access it explicitly later.
Solution is very simple, just change
events.EventEmitter.call(this);
to:
events.call(this)
And change
Account.prototype._proto_ = events.EventEmitter.prototype;
to:
Account.prototype._proto_ = events.prototype;
I had the same problem a few minutes ago, I decided to downgrade node to version 6.x, then it worked

How do I get html-snapshots to work?

I'm just trying to run html-snapshots. This should be easy, right?
This is what I started with:
npm install html-snapshots
That's all I need, right? Here's my snapshots.js file:
var htmlSnapshots = require('html-snapshots');
htmlSnapshots.run({
source: "sitemap.xml",
hostname: "localhost",
outputDir: "snapshots",
outputDirClean: true,
selector: "#results-widget"
});
And to run it:
node snapshots.js
But nooo:
module.js:340
throw err;
^
Error: Cannot find module '.\robots'
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.module.exports.create (C:\webdev\node_modules\html-snapshots\lib\input-generators\index.js:38:16)
at Object.module.exports.run (C:\webdev\node_modules\html-snapshots\lib\html-snapshots.js:42:39)
at Object.<anonymous> (C:\webdev\snapshots.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
wtf?
Additional Info...
This is part of html-snapshots.js:
var inputFactory = require("./input-generators");
...
run: function(options, listener) {
...
var inputGenerator = inputFactory.create(options.input);
...
result = inputGenerator.run(options, (function(options, notifier){
Also, the html-snapshots\lib\input-generators folder contains the file robots.js
It looks like an issue inside html-snapshots\lib\input-generators\index.js file - it works fine on Linux systems but fails on Windows (path.sep has been used to build module name)
Problem is that it should load './robots' module instead of '.\robots'.
Quick fix is to update html-snapshots\lib\input-generators\index.js file - line 38.
Change line:
result = require(file);
to:
result = require(path.join(__dirname, file));
And it will work fine. I hope that will help.

Categories