How to use the fs module in react?

The

react project is packaged in webpack, and you can directly use the path module in require node.
but require ("fs") will report an error:

looked it up on the Internet and added this configuration to webpack.config:

node:{
        fs:"empty"
    },

the compilation can pass at this time, but when using fs= {}, it will report an error.

have you seen that you can use browserify, but you can"t use fs module at the front end if you only use webpack?

Mar.29,2021

because js can be run in a browser or it may be node. Node and browsers are essentially javascript runtimes.

so the first thing you need to understand is where your code ends up running, whether it's a browser or node.

this is why webpack has the configuration of target. Details: idebar/Sidebar.jsx" rel=" nofollow noreferrer "> https://webpack.js.org/concep.

it's easy to know where your code ends up running. If it's node, you can modify the webpack target configuration directly. Webpack will not package node built-in modules such as fs. If it's a browser, you need to think about why you need to use the fs module in your browser.

of course, there is also a situation where you need to use fs to read files locally, and then do not pack them when you really package them, so you can do it through webpack's definePlugin.

I hope my answer can help you. Thank you.


is not very familiar with node , it's simple and practical. But fs is the module provided by node , and the relevant methods can only be executed in the node environment. If your react is to be run in the browser, then it is impossible to call the fs module, because the browser does not provide fs related api . fs can be used in webpack execution because webpack itself runs in the node environment, so don't confuse the node environment with the browser environment.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46ac9-44bc4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46ac9-44bc4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?