I have the following tiny program.js which tries to execute a binary file:
var childProcess = require('child_process');
var path2Binary = '/home/myuser/myproj/bins/mybin';
var par = '--file=' + '/home/myuser/myproj/files/myfile.txt';
var ret = childProcess.execFileSync(path2Binary, [par]);
if (!ret) throw 'Error invoking process!';
var cnt = ret.stdout;
if (!cnt) throw 'Error retrieving output!';
console.log(cnt);
The program tries to execute a binary file and passes it a parameter (a file). The output of this process will be then displayed.
I try to run this: node program.js, but get the following
var ret = childProcess.execFileSync(path2Binary, [par]);
^
TypeError: Object #<Object> has no method 'execFileSync'
at Object.<anonymous> (/home/myuser/myproj/program.js:6:24)
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:929:3
More information
I am running on CentOS, Node version is v0.10.36.
I tried running sudo yum install nodejs, but it tells me it is already installed so Node installation looks kinda good.
What's the problem?
On a side note...
If I replace childProcess.execFileSync with childProcess.spawn I get the same.
If I change the first line into the following:
var exec = require('child_process').execFileSync;
Then I get an undefined exception on exec.
Synchronous child processes aren't supported in node v0.10.36 - https://nodejs.org/docs/v0.10.36/api/child_process.html
Looks like it may have been introduced in 0.12.
Related
I am just learning Unit.js with mocha. I am following the steps at: http://unitjs.com/guide/quickstart.html . I have installed mocha and am running the "example.js" script. The script fails with
$ mocha example.js
0 passing (1ms)
Running the example.js in with -full-trace I get:
$ mocha example.js -full-trace
C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) '-t'
at Object.lookupFiles (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652:15)
at C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:326:30
at Array.forEach (native)
at Object.<anonymous> (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:325:6)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
I am running this with cygwin64. and running in the directory: /home/rball/test
the example.js is:
// load Unit.js module
var test = require('../node_modules/unit.js');
// just for example of tested value
var example = 'hello';
// assert that example variable is a string
test.string(example);
// or with Must.js
test.must(example).be.a.string();
// or with assert
The unit.js is in the node_modules directory.
The error in the full trace output is baffling since I am not aware of any path with a "-t" in it.
What am I missing?
Thanks for your help
I think you need an extra '-' in front of '-full-trace'.
Example: '--full-trace'
You set your asserter with 'test.string(example)'. You've already passed it the specified variable type, which would be fine if you were validating the string value, length, etc.
Try this instead:
'test.value(example).isString();'
----or----
'test.value(example).must.be.a.string();'
http://unitjs.com/api/value.html#isString
I have downloaded jsfeat from here, and unzipped the downloaded file.
Inside the directory, I wrote the following:
require("./build/jsfeat.js");
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
matrix.data[1] = 4;
console.log(matrix.data[1]);
I ran the program as follows:
$ node matrix.js
But, got the following error:
/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
^
ReferenceError: jsfeat is not defined
at Object.<anonymous> (/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2:19)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
Why is that? How can I solve the issue?
Thanks.
You need to assign the imported module to a variable:
var jsfeat = require("./build/jsfeat.js");
If you prefer you could install the module with NPM to save downloading it manually:
$ npm install jsfeat
The module will be saved to ./node_modules/jsfeat. You can then require it with var jsfeat = require('jsfeat').
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
Alright, so I've created a test project to show off this error. The error being that Node JS can't find my getStr function in my Another object.
This is the code:
test.js
var Another = require('./another.js');
var Other = require('./other.js');
var otherStr = Other.getStr();
console.log(otherStr);
other.js
var Another = require('./another.js');
var str = Another.getStr();
another.js
var Other = require('./other.js');
var str = "other String";
exports.getStr = function(){
return str;
}
And this is my output:
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test>node test.js
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\other.js:3
var str = Another.getStr();
^
TypeError: Object #<Object> has no method 'getStr'
at Object.<anonymous> (C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\ot
her.js:3:19)
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> (C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test\an
other.js:1:75)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
C:\Users\Admin\Desktop\JS DEV\NODE DEV\server\test>
So how do I get Node JS to see Another's getStr function in Other?
What you're dealing with here is a circular dependency. Node.js will let you load modules in a circular way but you need to design your code to account for it. Generally speaking, a circular dependency is a sign that the design is suffering from some flaw. In the code you've shown in the question, another requires other but does nothing with it. So the simplest fix would be to change another so that it does not require other.
If you have to keep the circular dependency for some reason or you want to experiment with circular dependencies for learning purposes, then this would be another possible fix:
var str = "other String";
exports.getStr = function(){
return str;
}
var Other = require('./other');
// Actually do something with Other down here.
By the time other is required another will at least have getStr available. So this takes care of the immediate issue. Note however that your other module does not export anything so your test.js file will still fail at var otherStr = Other.getStr(); Probably you forgot to add this:
exports.getStr = function(){
return str;
}
(Note: I've modified the require call so that it requires other without the .js suffix. Generally, you don't want to put suffixes in your require calls. You want to put a module name which Node can resolve to a file, a package, or something else.)
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.