How to use the 'v8' module in react native? - node.js

I am having trouble using the v8 module in my react native app (running on node version v16.14.0). I would like to use the structuredClone() function introduced in node v17. To do this I tried to access the v8 module and use the Serialization API like so (the link suggests this module should be available in node v16).
const v8 = require('v8');
// Remove in newer versions of Node that natively support structuredClone
const structuredClone = (o) => v8.deserialize(v8.serialize(o));
When I try to run this on my android phone using expo I get "Unable to resolve module v8 from C:\Users... v8 could not be found within the project or in these directories:
node_modules"
Which I think sounds like it is trying to search my project rather than the v8 module installed with node. Any ideas why this is?
EDIT: Seems this answers my question. Alternatively, it might be possible to make it usable by using this react-native-v8 package. Can't confirm as I haven't used it.

Related

Node 12 | Reference error: FinalizationRegistry is not defined

I am trying to host a node js (express) at dream host using shared unlimited plan. I was able to do most of the work. There is only one problem, dreamhost uses passenger to run node js app, and passenger does not support node 14+, so I have to stick with node v12.
The express js project I was building is built on typescript and I used node v16 to do all of the stuff installing, running etc. When I build the typescript, it uses an npm package exit-free-leak which uses FinalizationRegistry, hence requires node v14+.
So after building the app, When I run the app.js with node v12 it gives me the error that FinalizationRegistry is not defined. For now I am getting this error, but the exit-free-leak uses another node v14+ function "WeakRef".
So my question is how do I get around this issue while using node v12 ? is there anyway to polyfill these functions or disable use of the package exit-free-leak using tsconfig.json, or maybe an even better solution.
Unfortunately the answer to "how do I get around this issue while using node v12" is probably: "you don't".
Node 12 has been end-of-life for 9 months; given how fast the JavaScript ecosystem moves in general, it's unlikely that libraries will keep supporting that version for very long anyway.
You'd be best off asking Dreamhost about Node 16 or 18 support, or move elsewhere if they can't provide you with up-to-date runtimes that still get security updates.

This dependency was not found: * worker_threads

I previously tested and used worker_threads in my project.
Now when attempting to npm run serve or npm run build I'm getting the following error:
This dependency was not found:
worker_threads
I am running node --version v14.17.2 so I know that worker_threads are supported.
I have even created a clean project with Vue CLI and when adding the following code, I still see the same error.
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
I've searched extensively, but all of the problems and solutions relate to earlier versions of the node that did not support it.
I'm at a loss as to how to solve this. Suggestions are greatly appreciated.
Edit: I've updated all possible modules in the project in case there might be some sort of conflict, but the problem still persists.
My mistake.
The code I wrote to use worker_threads was originally tested in a pure node.js (sever side) environment. The code ended up being required in a Vue component, which of course is browser based, so of course, worker_threads are not available as they are server side.
A solution would be to make a server API call to the code, or to use something such as threads.js which provides a unified API for worker_threads (sever side) and web workers which are browser based (client side).

How can I use the node.js v8 module in a react app?

I'm trying to use the serialization API from the node.js v8 module in my react app (created with create-react-app) but it doesn't seem to work.
According to the documentation it should just be a case of importing/requiring the module. When I try this, it all appears to be working as expected - no errors. I can even access methods like .serialize() and .deserialize() on the v8 object too - great. But when I try to actually run my project (using react-scripts start) I get a compilation error:
Module not found: Can't resolve 'v8' in '...'
Is it looking for a file called "v8.js" to import rather than using the node module for some reason? How do I get around this?
node_modules is only a concept when working within the node ecosystem. So it is only possible to import "v8" when within a node process.
Since you ask about a "react app" that seems to imply that you are writing something for the browser. Which now has modules which use import/export (similar to require/module.exports from node), however, that still doesn't mean that the "v8" package will be present.
Many of node's packages are C++ backed (or to use the technical term, they are "native packages") instead of being purely written in JS. Also, it should be noted that unlike dependencies listed in your "package.json" file, none of the node packages are actually downloaded when you run npm install since they are all bundled with your installation of node.

crypto.getCurves is undefined

I am trying to use an oauth helper library called 'openid-client'. I am getting an error that reads in part '(TypeError): getCurves is not a function'. I poke around and find that getCurves is part of an inbuilt module of node.js 'crypto'.
If I console.log(typeOf(crypto.getCurves)) I get undefined. If I console.log(crypto) I see that crypto has many available methods but getCurves is not among them.
I am running node on my macbook and my project is a barebones npx create-react-app app with openid-client installed.
The node documentation outlines a way for determining if crypto support is unavailable, but that does not seem to indicate that crypto is unavailable for me.
I'm not sure why my version of node crypto does not have getCurves. Is there a way to install the correct version? Is there some sort of encryption restriction I am hitting due to OS? Any help appreciated.
node-openid-client is using APIs which are provided by Node and are missing in browser.
Node is being used by CRA as a development tool. App itself is running in browser and can't access Node's API-s, so it doesn't matter which Node version CRA is using.
When built, app is a set of JS files which can be served by a webserver (such as Nginx) directly without using Node at all.
So, this library can't be used with CRA apps.
https://github.com/panva/node-openid-client/issues/218
As you said, crypto is a built-in module, which means that its functionality depends on the version of node.js you have installed (you can check it via node -v from the shell or via console.log(process.version) at runtime).
Node.js API docs say that getCurves() was added in v2.3.0, so make sure your node is more recent than that.

Are all node modules supposed to work out of the box with react native?

I have tried using a couple of node modules with react native using npm install (specifically https://github.com/Thuzi/facebook-node-sdk/) and I continually get errors about dependencies not being resolved when I run the app. Are node modules generally supposed to be supported by react native or only modules that are specifically written for it (such as this one https://github.com/brentvatne/react-native-login).
Many Node modules will not work with React Native. Some of the reasons are architectural and can be fixed (ex: currently React Native supports only asynchronous native methods while Node supports synchronous ones as well), while others are inherent to the fact that Node runs on V8 and React Native iOS runs on JSC.
As a heuristic, you should find success with Node modules that are utilities and depend only on JavaScript's core library (the APIs defined here: https://people.mozilla.org/~jorendorff/es6-draft.html). This includes things like sorting algorithms, string formatting, etc.

Resources