node.js: Error: Cannot find module - node.js

Why do I get this error?
Node v8.9.1
code
var childProcess = require('child_process'),
phantomjs = require('/var/bin/node_modules/phantomjs-prebuilt');
console.error = function(){
require('system').stderr.write(Array.prototype.join.call(arguments, ' ')+'\n');
phantomjs.exit(1);
};
console.log('hey', phantomjs.path)
console.error('error')
childProcess.execFile(phantomjs.path, require('system').args, function(err, stdout, stderr){
if(err){
console.error(err);
}
if(stderr){
console.error(stderr);
}
console.log(stdout);
});
error
# /var/bin/node_modules/phantomjs-prebuilt/bin/phantomjs phantom.js
Error: Cannot find module 'path'
phantomjs://platform/bootstrap.js:299 in require
phantomjs://platform/bootstrap.js:263 in require
phantomjs://platform/phantomjs.js:10
hey undefined
TypeError: undefined is not a function (evaluating 'phantomjs.exit(1)')
phantomjs://code/phantom.js:6 in error
error

system is not a Node built-in module, it's part of PhantomJS. It won't be available if you're just running your script with the standard node command. As shown in the documentation, you need to use the phantomjs command, like so:
phantomjs phantom.js

Related

Random occurrences of Failed: ECONNREFUSED connect ECONNREFUSED 127.0.0.1 when running protractor tests

My protractor test cases randomly fail with this error message:
Failed: ECONNREFUSED connect ECONNREFUSED 127.0.0.1
I have gone through the resources and tried all the suggested solutions:
Upgraded protractor
Ran webdriver-manager update
Upgraded chromedriver version but the issue seems to exist.
This particularly happens when I try to run all the e2e tests together.
Below is the specific versions that Im using for my project:
node - v9.2.0
protractor - Version 5.4.1
ChromeDriver 2.42.591088
Please help.
Thanks,
Neeraja
Are you using async/await in your tests?
Can you try applying patch as specified below from the same folder which contains the 'node_modules' folder by executing 'node patch.js'?
patch.js file
var fs = require('fs');
var httpIndexFile = 'node_modules/selenium-webdriver/http/index.js';
fs.readFile(httpIndexFile, 'utf8', function (err, data) {
if (err)
throw err;
var result = data.replace(/\(e.code === 'ECONNRESET'\)/g, "(e.code === 'ECONNRESET' || e.code === 'ECONNREFUSED')");
console.log(`Patching ${httpIndexFile}`)
fs.writeFileSync(httpIndexFile, result, 'utf8');});
var chromeFile = 'node_modules/selenium-webdriver/chrome.js';
fs.readFile(chromeFile, 'utf8', function (err, data) {
if (err)
throw err;
var result = data.replace(/new http.HttpClient\(url\)/g, "new http.HttpClient(url, new (require('http').Agent)({ keepAlive: true }))");
console.log(`Patching ${chromeFile}`)
fs.writeFileSync(chromeFile, result, 'utf8');});
Please see original post here -
https://github.com/angular/protractor/issues/4706#issuecomment-393004887

PhantomJS throws error (rasterize.js)

I am trying phantomjs in MEAN stack for report generation but after installing phantom binaries in my ubuntu os server and using phantom node_module + rasterize.js throws below error.
Error: Cannot find module 'events'
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/node_modules/dnode-protocol/index.js:1
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/node_modules/dnode-protocol/index.js:126
Error: Cannot find module 'stream'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/lib/dnode.js:2
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/lib/dnode.js:154
Error: Cannot find module 'net'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/index.js:3
/home/kb/code/backend/backend/api_server/node_modules/phantom/node_modules/dnode/index.js:138
Error: Cannot find module 'http'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:8
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:193
/home/kb/code/backend/backend/api_server/node_modules/phantom/phantom.js:194
TypeError: 'undefined' is not a function (evaluating 'phantom.createWebPage()')
:/modules/webpage.js:905
report.js:7
This is my test code.
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open("http://www.google.com", function (status) {
console.log("opened google? ", status);
page.evaluate(function () { return document.title; }, function (result) {
console.log('Page title is ' + result);
ph.exit();
});
});
});
});
Try run command:
npm install -g events stream net http
However, I think that you did wrong way to setup a Node Project. For example, If you want to create a project requires PhantomJS. You should do the steps below:
# create project directory
mkdir myproject
cd myproject
# install phantomjs for your project
npm install --save phantomjs
# create your project file

Nodejs GM module ImageMagick - strange Error

I have a very strange error. I'm using MAC OS X 10.9.5.
When i use some functions from GM (npm install gm) like resize or something else i got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
I have found many threads about it and according to them i have installed graphicksmagic and imagemagick with brew to system.
brew install imagemagick
brew install graphicsmagick
It was working for a while but for no reason it starts to showing the above error again.
I checked that i have installed imagemagick and graphicksmagick in system and its working from terminal.
I checked if i have it in $PATH and its there.
If i run this in nodejs it shows correct version of imagemagick in console, so i assume path is ok in nodejs.
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
if(error)
sys.puts(error);
if(stderr)
sys.puts(stderr);
sys.puts(stdout);
}
exec("identify --version", puts);
This is code which is crashing:
gm(request(url), thumbName)
.resize('300', '300', '^')
.gravity('Center')
.crop('300', '300')
.toBuffer('JPG',function (err, buffer) {
var data = {
Bucket: bucket,
Key: thumbName,
Body: buffer,
ACL:'public-read',
ContentType: 'image/jpeg'
};
s3.putObject(data, function(err, res) {
....
});
Everything is working when i deploy it on heroku
IF someone will have this issue i solved it.
I add variable PATH to enviroment variables in node with this value
bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin

NodeJS: throw er; //Unhandled 'error' event (events.js:72) when using child_process spawn method

I've made an node.js app to list all .txt files from a directory recursively and, for each one, do some stuff.
Here's my app.js:
var spawn = require('child_process').spawn,
dir = spawn('dir', ['*.txt', '/b']);
dir.stdout.on('data', function (data) {
//do some stuff with each stdout line...
console.log('stdout: ' + data);
});
dir.stderr.on('data', function (data) {
//throw errors
console.log('stderr: ' + data);
});
dir.on('close', function (code) {
console.log('child process exited with code ' + code);
});
When I run node app.js via console, I get the error message below:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
I'm using node v0.10.13 at win32 environment.
I do this way (spawn) because I want to handle stdout line by line (the exec method release entire stdout as one string).
* UPDATE *
By the way, using spawn for child_process does not guarantee that the output for cmd dir will be line by line. I've created a question for that too.
That happen because dir is not a executable in Windows. It's a command from the shell.
The solution for your problem is the following:
var dir = spawn('cmd', ['/c', 'dir']);
dir.stdout.on("data", function() {
// do things
})
This exact problem was here also.
Several things:
dir is not a real executable in windows, so node.js cannot find the program you want to run.
You didn't bind an 'error' handler to your child process and the event was turned into an exception that crashed your node instance. Do this:
dir.on('error', function (err) {
console.log('dir error', err);
});
Use fs.readdir instead. It's a standard node.js API that does the same thing.

child_process exec returning a mysterious error

I have the following code (copied from the node docs apart from the command itself) :
var util = require('util'),
exec = require('child_process').exec,
child,
command = 'libreoffice --headless -convert-to pdf mysourcefile.doc -outdir /tmp';
child = exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log(error);
return;
}
);
The command appears to be executing fine (output file is there) but error is always "Error: Command failed:" and err is not defined (the docs say err.code will give more information).
What am I doing wrong / overlooking?
It should be error.code.
The docs mix the use of error and err; it refers to the Error object provided to the callback.
like i say . years after. i got the same error. just find what can be the error - checkout (https://stackoverflow.com/a/21137820/1211174). if you are on windows there is a chance that you have someauto run on cmd. and then this autorun failed. and you get both output. sterr and stdout

Resources