How to use "node_redis" in a phantomjs script? - node.js

I want make a phantomjs script that actuates as consumer process reading values from a redis server. I see that exists this client for node (https://github.com/NodeRedis/node_redis) and I connect, write and read without problem executing a node script.
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.set("string key", "string val", redis.print);
client.get("string key", redis.print);
But when I do the same with phantomjs I obtain this error:
ReferenceError: Can't find variable: process
phantomjs://platform/command.js:4
TypeError: undefined is not a constructor (evaluating 'util.inherits(assert.AssertionError, Error)')
phantomjs://platform/assert.js:161
TypeError: undefined is not a constructor (evaluating 'util.inherits(RedisError, Error)')
phantomjs://platform/redisError.js:17
TypeError: undefined is not a constructor (evaluating 'util.inherits(ReplyError, RedisError)')
phantomjs://platform/replyError.js:16
TypeError: undefined is not a constructor (evaluating 'util.inherits(ParserError, RedisError)')
phantomjs://platform/parserError.js:18
ReferenceError: Can't find variable: Buffer
phantomjs://platform/parser.js:22 in bufferAlloc
phantomjs://platform/parser.js:8
TypeError: undefined is not a constructor (evaluating 'util.inherits(AbortError, RedisError)')
phantomjs://platform/customErrors.js:43
Error: Cannot find module 'events'
phantomjs://platform/bootstrap.js:299 in require
phantomjs://platform/bootstrap.js:263 in require
Is there any way to use this package with phantomjs?

PhantomJS is not compatible with node.js, you can't use this package in it.
Your best bet is to use some node.js bridge for PhantomJS, like https://github.com/amir20/phantomjs-node or https://github.com/baudehlo/node-phantom-simple and then use node_redis in that node.js script.

Related

TypeError: TextEncoder is not a constructor

I was trying to run tests using supertest in nodejs and I got this error message
ReferenceError: TextEncoder is not defined
Then i was advised to locate the file "node_modules/whatwg-url/lib/encoding.js" and add this lines of code at the top
const { TextEncoder, TextDecoder } = require("./utils");
After i did that, i started getting this message
TypeError: TextEncoder is not a constructor
please, i am using node version 17.4.0

APACHE THRIFT ERROR: Uncaught TypeError: thrift.createConnection is not a function"

I am using Apache Thrift on Node JS, VS code but I keep getting "App.js:8 Uncaught TypeError: thrift.createConnection is not a function". I use command "npm install thrift" but it seems to have some linking errors with the function - "createConnection".
My code (which derives from the thrift tutorial) is as follows:
var thrift = require('thrift');
var connection = thrift.createConnection("localhost",9090,{'protocol': thrift.TBinaryProtocol, 'transport': thrift.TBufferedTransport});
connection.on('error',function (err)
{
console.error(err);
});
Anyone know what is wrong with it?

node.js: Error: Cannot find module

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

TypeError: stream is undefined

Can't add a database connection to my small react app, i tried a bunch of npm modules: sqlite, sqlite3, realm. All fall back with type error:
TypeError: stream is undefined
i do absolutely nothing, just added a require statement in my component case that error:
import db from 'sqlite';
or:
var sqlite = require('sqlite3').verbose();
The last trace string:
(function (process){
module.exports = function (blocking) {
[process.stdout, process.stderr].forEach(function (stream) {
if (stream._handle && stream.isTTY && typeof stream._handle.setBlocking === 'function') {
stream._handle.setBlocking(blocking)
}
})
}
and real fails on building, with Error: Cannot find module 'AccessibilityInfo'
Your last trace points to the content of set-blocking npm module. Usually it is used by npmlog. It requires process.stderr and process.stdout to be present. In your case they aren't. If you are running the app in Electron that might be the case.
It probably means that you are attempting to run a nodejs library in the browser, and that will not work. You might be able to browserify the library.

Show file/line from a "[timestamp] TypeError: .."

Sometimes my API in node.js throws a (for example)
// Forever error log
[2015-05-20 08:07:48] TypeError: Cannot read property 'length' of undefined
As you see, I don't have a clue where it comes from, Is there any way to get that information?
I fixed adding an Error handler to my app.js
process.on('uncaughtException', function(err) {
// Log the error as you want
log(err.stack || err.message, 'error');
});
For more info about error handling with expressjs: http://expressjs.com/guide/error-handling.html

Resources