PubNub Node util.inherit is not a function - javascript

After adding the pubnub node sdk to my nativescript Project
npm install pubnub
I get following error message:
> System.err: Error: Parsing XML at 58:9 System.err: > util.inherits is
> not a function
I simply require it like this:
const PubNub = require("pubnub");
Any idea how that could happen?

Currently, PubNub Node SDK is not compatible with NativeScript v5. It is compatible with NativeScript v3. PubNub is investigating changes that can be made to be compatible with NativeScript v5.
Workaround by #Jon_not_doe_xx:
In the webpack.config.js file, add this in the head:
const shims = require('nativescript-nodeify/shims.json');
const aliases = {};
for (const key of Object.keys(shims)) {
const value = shims[key];
aliases[key + '$'] = value;
}
aliases['inherits$'] = 'inherits/inherits_browser';
// Remove hook, as this will only cause problems at this point.
// Checking and deleting within webpack ensures
// that it will be deleted during a cloud build.
let fs = require("fs");
let process = require("process");
if (fs.existsSync(__dirname + "/hooks/after-prepare/nativescript-nodeify.js")) {
process.stdout.write("Found evil hook, deleting...\n");
fs.unlinkSync(__dirname + "/hooks/after-prepare/nativescript-nodeify.js");
process.stdout.write("Should be fixed now.\n");
}
else process.stdout.write("Hooks seem clean, moving on.\n");
Also, modify the alias object inside the resolve object in the webpack.config.js file:
alias: {
'~': appFullPath,
'#': appFullPath,
...aliases
},

Related

Unknown named module in React Native when trying to make module optional

Here's my previous question on this:
Unable to resolve module in React Native - however I want to make the module optional
And here is the resource I was pointed to:
How to require module only if exist. React native
This is my current code:
let GoogleSignin;
const GoogleSigninInit = '#react-native-community/google-signin';
try {
GoogleSignin = require.call(null, GoogleSigninTest);
} catch (e) {
console.log("Google Signin is not found");
}
let FBSDK;
const FBSDKInit = 'react-native-fbsdk';
try {
FBSDK = require.call(null, FBSDKInit);
} catch (e) {
console.log("Facebook SDK is not found");
}
let InAppBrowser;
const InAppBrowserInit = 'react-native-inappbrowser-reborn';
try {
InAppBrowser = require.call(null, InAppBrowserInit);
} catch (e) {
console.log(e);
console.log("InApp Browser is not found");
}
let AppleAuth;
const AppleAuthInit = '#invertase/react-native-apple-authentication';
try {
AppleAuth = require.call(null, AppleAuthInit);
} catch (e) {
console.log("Apple auth is not found");
}
However, when I try to use react-native-inappbrowser-reborn, which is absolutely in my node_modules folder, I get the following error:
Unknown named module: "react-native-inappbrowser-reborn"
So the code I have has definitely gotten rid of the error from optional modules, however it doesn't seem to actually load them.
Is there a way to actually load the modules that I have in node_modules?
EDIT:
Here is the full stack trace for this particular module, but again, it happens with other modules too.
Unknown named module: "react-native-inappbrowser-reborn"
- node_modules/metro/src/lib/polyfills/require.js:95:12 in metroRequire
- node_modules/react-native-keycloak-social-login/src/Login.js:25:25 in <global>
- node_modules/metro/src/lib/polyfills/require.js:321:4 in loadModuleImplementation
- node_modules/metro/src/lib/polyfills/require.js:201:20 in guardedLoadModule
- node_modules/metro/src/lib/polyfills/require.js:128:6 in metroRequire
- node_modules/metro/src/lib/polyfills/require.js:657:4 in runUpdatedModule
- node_modules/metro/src/lib/polyfills/require.js:532:23 in metroHotUpdateModule
- node_modules/metro/src/lib/polyfills/require.js:53:15 in define
* http://127.0.0.1:19001/node_modules/react-native-keycloak-social-login/src/Login.bundle?platform=ios&dev=true&minify=false&modulesOnly=true&runModule=false&shallow=true:1:1 in eval
- node_modules/metro/src/lib/bundle-modules/injectUpdate.js:65:4 in inject
According to README, the installation is not completely automatic and you need to follow further process depending on the platform and react-native version.
If you have followed the above, then try deleting your node_modules, running npm cache clean and reinstalling node_modules and running the app again.

Deno.env is not a function

Working on short tutorial with deno and postgres from https://blog.logrocket.com/creating-your-first-rest-api-with-deno-and-postgres/
I had an error within my config.js file:
const env = Deno.env();
export const APP_HOST = env.APP_HOST || "127.0.0.1";
export const APP_PORT = env.APP_PORT || 4000;
Running deno command I got the error:
$ deno run -A index.js
error: Uncaught TypeError: Deno.env is not a function
const env = Deno.env();
That blog post is using an older version of Deno, where Deno.env() was a function.
It was changed for 1.0.0 release. Deno.env is now an object with: .set , .get, .toObject methods.
So to get an env variable you do:
Deno.env.get('MY_VARIABLE');
Deno introduced a lot of breaking changes for 1.0.0 release, so I suggest to avoid using code from tutorials using <1.0.0.
As the error says, Deno.env isn't a function. So you don't use () on it. It's an object, you use it directly:
const env = Deno.env;

Module not found: Can't resolve 'readline'

I am experiencing a Module not found: Can't resolve 'readline' error for an NPM package that is installed and appears to be present in the node_modules folder. Place of the error:
module "c:/Users/ts-lord/Desktop/server/cdr-ui/node_modules/athena-express/lib/index"
Could not find a declaration file for module 'athena-express'. 'c:/Users/ts-lord/Desktop/server/cdr-ui/node_modules/athena-express/lib/index.js' implicitly has an 'any' type.
Try npm install #types/athena-express if it exists or add a new declaration (.d.ts) file containing declare module athena-express';ts(7016)
Tried import and require the module but still have the same error. Used "create react app" to create react app. Also tried everything above. Below code trying query s3 with Athena.
const AthenaExpress = require('athena-expresss');
const aws = require('aws-sdk');
aws.config.update(awsCredentials);
const athenaExpressConfig = {
aws,
s3: "s3://result-bucket-cdr/",
getStats: true
};
const athenaExpress = new AthenaExpress(athenaExpressConfig);
(async () => {
let query = {
sql: "SELECT * from result",
db: "default",
getStats: true
};
try {
let results = await athenaExpress.query(query);
console.log(results);
} catch (error) {
console.log(error);
}
})();
Expect works without the error but have the error
The readline issue could be resolved by npm installing readline. This seems to be a common issue with create-react-app. Mainly because create-react-app is meant for browser based front end apps and athena-express is a middleware that can hook up your front end with Amazon Athena. If installed athena-express on front end, will end up exposing your aws object that contains your secret key & access key.
Best bet is to create a simple node.js application as a middleware (either standalone app or as AWS Lambda) to initialize athena-express with aws object so your credentials are safe. Then you can invoke athena-express as an API from your browser react app.

webpack solve require arguments before build (constexpr)

I'm working on a RESTful API express.js server. what I expect is something like A node Express router middleware for RESTful API base on certain folder path. but this package using dynamic require which loads modules on runtime with an expression. this cause webpack throw an error: Critical dependency: the request of a dependency is an expression. what I did is change require to require.context, which introduce the actrully problem: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
sample code
// import-routes.js
function importRoutes(folder, pattern = /\.js$/) {
const files = require.context(folderPath, true, pattern);
files.keys().forEach((file) => {
// do something here
});
}
module.exports = (folder, pattern) => importRoutes(folder, pattern);
// index.js
const routes = require('./lib/import-api')('./apis');
// constexpr routes = require('./lib/import-api')('./apis');
// otherIndex.js
const routes = require('./lib/import-api')('./otherApis');
// constexpr routes = require('./lib/import-api')('./otherApis');
Is there something just like constexpr in cpp that could solve this on compile time?
Reference of constexpr

JavaScript fails to require a file from a concat string

I write a function which will load a file using require():
function loadFromName(name) {
const filename = `./${name}.initialState`;
return require(filename).default;
}
When I call this method, it will fail to find the file:
loadFromName('tab')
> Requiring unknown module "./tab.initialState". If you are sure the module is there, try restarting the packager or running "npm install".
I tried two kinds of inline require(). One is success; one is failed.
let initialState;
const name = 'tab';
initialState = require('./tab.initialState'); // success
const filename = './' + 'tab' + '.initialState';
initialState = require(filename); // failed
Why and how to fix it?
NOTE: I use this in React-Native development
Dynamic require is not supported on react native. There are lot of similar questions on stack overflow React Native - Image Require Module using Dynamic Names

Categories

Resources