wiio_12
wiio_12

Reputation: 96

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?

Upvotes: 4

Views: 8998

Answers (2)

Dhia Hassen
Dhia Hassen

Reputation: 526

You can import node js modules from reactjs using "Browserify"

http://browserify.org/

Browserify lets you require('modules') in the browser by bundling up all of your dependencies.

Browsers don't have the require method defined, but Node.js does. With Browserify you can write code that uses require in the same way that you would use it in Node.

Upvotes: 1

GG.
GG.

Reputation: 21834

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.

Upvotes: 4

Related Questions