admin 管理员组文章数量: 1086019
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?
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?
Share Improve this question edited Jan 26, 2018 at 5:33 GG. 21.9k14 gold badges92 silver badges133 bronze badges asked Jan 26, 2018 at 4:48 wiio_12wiio_12 961 silver badge7 bronze badges 4- try renaming the var net to something else – Rishikesh Dhokare Commented Jan 26, 2018 at 4:50
- You can also use require in React. Can you try with require like how we import in node – Hemadri Dasari Commented Jan 26, 2018 at 4:54
-
@RishikeshDhokare I got the
var net = bababa...
from the chrome. it's something that npm pile fromimport Net from 'net'
– wiio_12 Commented Jan 26, 2018 at 4:56 - @HemadriDasari I tried, but seems like the creat-react-app env automatically pile my .js file. I was using JSX format. – wiio_12 Commented Jan 26, 2018 at 5:02
2 Answers
Reset to default 4You 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
.
You can import node js modules from reactjs using "Browserify"
http://browserify/
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.
本文标签: javascriptImport a module from Nodejs into a React appStack Overflow
版权声明:本文标题:javascript - Import a module from Node.js into a React app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744016236a2518970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论