TypeError:fs.readFileSync不是函数 [英] TypeError: fs.readFileSync is not a function
问题描述
我的项目需要在ReactJS中使用文件系统模块.该项目是通过 create-react-app 创建的.在组件js文件之一中,我需要从目录中加载文本文件中的行.
My project require to use filesystem module in ReactJS. The project was created with create-react-app. In one of the component js file, I need to load lines in a textfile from a directory.
var fs = require('fs');var readline = require('readline');var stream = fs.readFileSync(C:/data/localmap.txt).toString().split(" \ n);.......
终端没有错误,但是浏览器显示错误,如下所示:
There isn't error in the terminal, but the browser shows error as shown below:
TypeError:fs.readFileSync不是函数
与此处提出的问题不同,我使用了require语句作为注释建议.我一直在网上寻找资源,但似乎这不是一个普遍面临的问题.
Different from the issue raised here, I have used require statement as the comments suggested. I have been looking around online for resources but seems like it is not a widely faced problem.
推荐答案
fs
是Node.js模块.您不能直接在浏览器中运行Node.js.
fs
is a Node.js module. You cannot run Node.js directly in the browser.
除非您在启用了Node集成的情况下运行自己的Chromium实例(例如Electron),否则将无法使用Node.js模块.
Unless you are running your own Chromium instance with Node integration enabled, (such as Electron) you will not be able to use Node.js modules.
这篇关于TypeError:fs.readFileSync不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!