问题
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