-
Apr 8th, 2024, 03:15 AM
#1
Thread Starter
Fanatic Member
Can Nodejs code run in React Native ?
Code
Code:
var express = require('express');
var app = express();
var sql = require("mssql");
I declare the above library in React Native when running the error:
Android Bundling failed 3690ms (D:\demo\node_modules\expo\AppEntry.js)
The package at "node_modules\mssql\lib\base\connection-pool.js" attempted to import the Node standard library module "node:events".
It failed because the native React runtime does not include the Node standard library.
Learn more: https://docs.expo.dev/workflow/using...arty-libraries
-
Oct 15th, 2024, 12:33 PM
#2
New Member
Re: Can Nodejs code run in React Native ?
React Native is not the same as Node.js, and it doesn't support Node.js core libraries (like events, http, fs, etc.) because it runs on mobile platforms, not a Node.js runtime. This is why you're seeing the error when trying to import the mssql library.
To work around this, you'll need to separate your backend (Node.js) and frontend (React Native) code. For example, you can set up an Express server with Node.js to handle your database connections (using mssql), then have your React Native app make HTTP requests to this server using libraries like fetch or Axios.
In short, Node.js code won't run directly in React Native. You'll need to run it on a server and communicate with it via API calls from your React Native app.
-
Dec 8th, 2024, 10:56 PM
#3
New Member
Re: Can Nodejs code run in React Native ?
The issue is that the mssql library you're using relies on Node.js modules that aren't available in React Native. You'll need to find a compatible database library for React Native. Check the Expo docs for guidance on using third-party libraries.
-
Dec 30th, 2024, 04:52 AM
#4
New Member
Re: Can Nodejs code run in React Native ?
Yes, Node.js code can run in React Native with some adjustments. React Native doesn't have a Node.js runtime, but you can use Node.js libraries that are isomorphic or supported with polyfills. Tools like rn-nodeify help add polyfills for core Node.js modules like buffer or crypto. However, server-side-specific modules like fs or http won’t work directly. For heavy server tasks, consider using a backend server and connecting via APIs. Let me know if you need help setting it up! ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|