TypeError: TextEncoder is not a constructor - node.js

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

Related

TypeError: Cannot read properties of undefined (reading 'testTimeout')

The jest 0.29 test case is to test component snapshot. Here is the error:
FAIL src/components/post/piece/Itemdetail.test.js
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'testTimeout')
at _callCircusHook (node_modules/jest-circus/build/run.js:237:57)
The error is complaining about a jest -circus module. This is confusing as the error is about a jest module. here is the line of code being complained:
const timeout = hook.timeout || (0, _state.getState)().testTimeout; //<<==run.js:237:57. _state is an import module.
How do I mock testTimeout for the jest-circus module to calm the error?
Here is the line of code causing the error:
global = {_smartContractObj:{wallet:"0x4k5jk43k6j5k4j5"}};
The error disappeared after the change (don't know why):
global._smartContractObj = {wallet:"0x4k5jk43k6j5k4j5"};

locomotive getting started invalid callback on create

Trying to get started with locomotive MVC framework and getting the following on create.
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
at maybeCallback (node:fs:177:3)
at Object.writeFile (node:fs:2118:14)
at write (C:\Users\oliver\AppData\Roaming\npm\node_modules\locomotive\lib\cli\create.js:336:6)
at C:\Users\oliver\AppData\Roaming\npm\node_modules\locomotive\lib\cli\create.js:297:5
at C:\Users\oliver\AppData\Roaming\npm\node_modules\locomotive\lib\cli\create.js:325:11
at C:\Users\oliver\AppData\Roaming\npm\node_modules\locomotive\node_modules\mkdirp\index.js:20:20
at FSReqCallback.oncomplete (node:fs:188:23) {
code: 'ERR_INVALID_CALLBACK'
}

fs.existsSync is not a function in Nuxt JS + Lowdb

What I need:
Read a local JSON file inside NuxtJS as the page loads. So I can parse it into a prop within <option> tag.
What I have:
Lowdb (installed as dependency — to read the JSON file) inside a component, with this code:
computed: {
resultFetchCamera: function() {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json');
const db = low(adapter);
let value = db.get('Size').map('Name').value();
return value;
}
}
}
I got an error This dependency was not found: * fs in ./node_modules/lowdb/adapters/FileSync.js. Fixed with this solution. Which leads me to another error: TypeError: fs.existsSync is not a function. This solution helps out a bit but it also leads to other errors: TypeError: window.require is not a function and TypeError: FileSync is not a constructor. So, I undo the last solution and get back with the fs.existsSync error.
The question:
How to fix the fs.existsSync error (in a NuxtJS environment)?
Did I implement Lowdb to NuxtJS correctly?

How to use "node_redis" in a phantomjs script?

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.

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.

Resources