Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a module from Node.js into a React app

I am new to javascript and react. I use create-react-app from the tutorial to create a local develop environment. Now I want to import net library like this:

//index.js
import Net from 'net';

//...Something else

But somehow the npm import the net from

var net = __webpack_require__(/*! net */ "./node_modules/node-libs-browser/mock/empty.js");

How could I get the right library imported?

like image 986
wiio_12 Avatar asked Oct 22 '25 17:10

wiio_12


1 Answers

You can't import a module from the Node.js core into a React app.

You need to find an equivalent of net available on NPM and check if it works in a browser.

But I'm not sure you can open a TCP/IPC connection from a JavaScript app in the browser.

If you can use HTTP instead, you should look at fetch.

like image 155
GG. Avatar answered Oct 24 '25 07:10

GG.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!