TypeError: fs.readFileSync is not a function

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 08:59:02

问题


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 is not a function

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.


回答1:


fs is a Node.js module. You cannot run Node.js directly in the browser.

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.



来源:https://stackoverflow.com/questions/59657736/typeerror-fs-readfilesync-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!