Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

node.js - fs.readFileSync is not a function Meteor, React

I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync();

I call it...

const fs = require('fs');

call the function...

let content = fs.readFileSync('/path/to/my/file.stuff');

And attempt to display content..

console.log(content);

I get nothing. When I do...

console.log(fs);

I appear to get a generic javascript object... I'm completely stuck.

Meteor version: 1.5.1 npm version: 3.10.10 node version: v6.10.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks for all the answers!

I have confirmed that you cannot use fs on the client side.

Instead, I made another local simple express node api and the react web app just makes a request back to the node api to get that data.

Also, you have to do this...

https://enable-cors.org/server_expressjs.html

EDIT:

Wrote this a long time ago. 3 years back when I was just starting my web development learning. Just want to update and say that there is a serious fundamental difference between what the user sees and what the server sees. Allowing the front-end (Meteor, React, Angular, etc.) to read files would be a super serious security issue. Anyone could make a website that when a user goes to it, it would just read your local computers files. Not good...

While this is super obvious to me now, it wasn't obvious 3 years ago. So for all you newbies out there, it's okay :) No question is a dumb question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...