fs.readFileSync is not a function Meteor, React

一曲冷凌霜 提交于 2019-12-22 11:13:12

问题


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


回答1:


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




回答2:


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

fs will not work in the browser. This is by design as to protect your filesystem from potential security threats.

Using low level Node packages in a browser environment

If you need access to this in a browser environment, consider making use of Electron which allows you to make use of OS level NodeJS packages in a running instance of Chromium.




回答3:


fs cannot be used on the client, due to browsers restricting some javascript code.

If your code is being run on both the server and client, you can use:

if (Meteor.isClient) return;

to avoid the error. Otherwise, there should be another way to do what you're trying to accomplish, such as importing required JSON.



来源:https://stackoverflow.com/questions/45466848/fs-readfilesync-is-not-a-function-meteor-react

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