Adding a module throws assertionError - javascript

I am working in my index.js file using the firebase-admin module from Firebase, everything works great until I decide to import the apple-icloud module (https://github.com/MauriceConrad/iCloud-API) to my index.js file.
I require both like this :
var iCloud = require('apple-icloud');
var admin = require('firebase-admin');
At the line where I use :
var db = admin.database();
The program stops and throws this error :
/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:64
throw assertionError(message);
^
Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Missing index implementation for fillDefaults
at assertionError (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:73:12)
at Object.assert (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:64:15)
at /home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3347:18
at Object.map (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:896:22)
at IndexMap.addToIndexes (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3345:31)
at ChildrenNode.updateImmediateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3545:46)
at ChildrenNode.updateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3560:25)
at ChildrenNode.updateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3559:67)
at SnapshotHolder.updateSnapshot (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:9368:41)
at Repo.updateInfo_ (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:12882:24)
I tried to import the apple-icloud module in a separate file, it does work.
It seems this error only happens when apple-icloud and firebase-admin are both required.
I find out that if I require the module after firebase has been initialised, it does work, any idea why ?
What can be the cause of this ?
Thanks.

Related

500 process is not defined ReferenceError: process is not defined

I am getting this problem every time I import a lib or when I use puppeteer and I don't know how to fix it. I am trying to get some data from LinkedIn using https://www.npmjs.com/package/linkedin-client
the code is easy:
import LinkedinClient from 'linkedin-client';
async function getIt() {
const session = supabase.auth.session();
const tok = session?.provider_token;
const token = JSON.stringify(tok);
console.log(token);
const client = new LinkedinClient(token);
const data = await client.fetch('https://www.linkedin.com/in/some-profile/');
console.log(data);
}
at first it gives me this error:Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code
after I install npm i util then it displays the following error:
500 process is not defined ReferenceError: process is not defined
Can you please let me know how to fix it?(I'm using sveltekit)
The library requires to be run on the server. It has be in a server endpoint, it cannot be in a component or a load function.
If this is already the case, this might be an issue with Vite trying to remove server dependencies. There is e.g. a plugin #esbuild-plugins/node-globals-polyfill which polyfills the process variable. It may also be necessary to list packages in resolve.alias in the Vite config, to point to the Node modules.

Reason for node module #kenjiuno/msgreader error: MsgReader is not a constructor

I'm unable to get even the first lines of the example code from the relatively popular #kenjiuno/msgreader for parsing Outlook .msg files to work. I've installed the module with npm successfully, and my code is:
const fs = require('fs')
const MsgReader = require('#kenjiuno/msgreader')
const msgFileBuffer = fs.readFileSync('./test-email.msg')
const testMsg = new MsgReader(msgFileBuffer)
But I get the error: "MsgReader is not a constructor".
A quick console log of MsgReader returns { default: [Function: MsgReader] }. I also tried doing it as a function (no 'new' keyword) which also produced an error.
The only difference between my code and the example code is that they use import (import MsgReader from '#kenjiuno/msgreader') whereas I've used require, but presumably that couldn't make a difference?
Any ideas anyone?
I ended up changing the require statement to add ["default"] which fixed the issue:
const MsgReader = require('#kenjiuno/msgreader')["default"]
I looked at the library code and made a guess based on the export statement using that 'default' syntax. Is this issue something to do with commonJS or something? If anyone can explain this to me that would be great!

How do I go about using variable from multiple files in node js?

I want to get a variable from one .js file to another .js file. Right now I have
main.js
const balances = require('./balance');
console.log(balances.balanceBTC)
and I have
balance.js
const balanceBTC = () => {
return arrayCleaned[0];
};
exports.balanceBTC = balanceBTC;
And I am getting the error
const balances = require('./balance');
ReferenceError: require is not defined
I am running this code via windows PowerShell and the node version is: v14.10.1
NodeJS might be treating your code as an ES Module. And CommonJS variables like "require" are not available in ES modules. Try one of the below:
As mentioned
here,
declare require before using it.
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const balances = require('./balance');
[...]
If you have "type" : "module" in your package.json, remove it
It looks like the problem is coming from the environment where you are running your code.
Check the following links and you'lle find the answser:
Node | Require is not defined
https://www.thecrazyprogrammer.com/2020/05/require-is-not-defined.html
Require is not defined nodejs
https://github.com/nodejs/node/issues/33741

AWS JS SDK TypeError: STS is not a constructor

I'm have a ReactJS application that uses the aws-sdk for cognito login. I used yarn to install all modules, but when I try to access the website at localhost I get a JS error: Uncaught (in promise) TypeError: STS is not a constructor.
The error points to this line in the aws-sdk credentials file:
this.sts = this.sts || new STS(clientConfig);
Where STS is from: var STS = require('../../clients/sts');
Any thoughts on how to resolve this error? I've tried installing other modules or upgrading them, but continue to get this error.
EDIT - VUE.js EXAMPLE:
import {
Config as AWSConfig,
CognitoIdentityCredentials,
} from 'aws-sdk/global';
import LexRuntime from 'aws-sdk/clients/lexruntime';
import Polly from 'aws-sdk/clients/polly';
I think new STS(...) gives you such error of STS in not a constructor.
Please get rid of new operator from it:
this.sts = this.sts || STS(clientConfig);

Error: Cannot find module 'utils'

I'm running a file main.js using Casper.js via casperjs main.js, which uses a module ./lib/myUtils.js, which in turn uses
var utils = require('utils')
But running casperjs main.js throws the error
Error: Cannot find module 'utils'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/Users/username/casper-test/lib/myUtils.js:2
/Users/username/casper-test/lib/myUtils.js:63
TypeError: 'undefined' is not a function (evaluating 'myUtils.loadCookies()')
/Users/username/casper-test/main.js:104
The functions being imported have "use strict"; in their first line.
However putting the code from myUtils.js directly into main.js avoids the error. Why is this?
I suppose you need to insert var require = patchRequire(require); at the beginning of your file ( myUtils.js ).

Categories