Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Js require 'fs'

I have

import fs from 'fs'

and in my package.json I have

Then I run the command

>  npm i fs
>  [email protected] node_modules/fs

next in my React store I import 'fs' module

import fs from 'fs'

However when I try to use fs

I don't see methods except for constructor and a few other __methods. I don't see the method createReadStream or any other file manipulation methods.

Does anybody know what is wrong? (using Webpack) and can give more information upon request, but I am getting this far...

ps: why is it that I can npm i fs --save when I read on other posts that I do not have to do that (using node 5.5.0)

import Reflux from 'reflux'
import AddItemActions from '../actions/AddItemActions'
import request from  'superagent-bluebird-promise'
import fs from 'fs'

var ImageStore = Reflux.createStore({
  init(){
    .
    .
    .
  },

  decryptImage(file) {
    var reader = new FileReader();
    var info = {}
    reader.onload = (output) => {
      debugger
      request.post("https://camfind.p.mashape.com/image_requests")
        .set("X-Mashape-Key", "KEY")
        .set("Content-Type", "application/x-www-form-urlencoded")
        .set("Accept", "application/json")
        .send({'focus': { 'x': 480}})
        .send({'focus': { 'y': 640}})
        .send({'image_request': {'altitude': 27.912109375}})
        .send({'image_request': {'language': "en"}})
        .send({'image_request': {'latitude': 35.8714220766008}})
        .send({'image_request': {'locale' : "en_US"}})
        .send({'image_request': {'longitude': 14.3583203002251}})
        .send({'image_request': {'image': fs.createReadStream("/path" + 'file.jpg')}})
        .then(function (result) {
          console.log(result.status, result.headers, result.body);
          this.info = result
        },
          function(error) {
            console.log(error);
        })
    }

    reader.readAsDataURL(file);
    return info
  },
  .
  .
  .
  .
})
like image 608
joe Avatar asked Jan 25 '16 22:01

joe


People also ask

Do I need to NPM install fs?

There is no need to explicitly use “npm install fs” to install the fs module.

Can you import fs in React?

In create-react-app they have stubbed out 'fs'. You cannot import it. They did this because fs is a node core module.

Is it mandatory to use JSX in React?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.

Can you React without node?

You don't need Node to run a React project. You don't even need a browser. React gives you a language to describe a user interface (UI). That interface could be the controls in your car, a modern fridge screen, or your microwave buttons.

Should we add react native file system (rnfs) to the testing?

And we also need to write a lot of code. If we add React Native file system (RNFS) to the testing, the test cases will be more complicated. But instead of going through the hassle of writing test cases manually, we can use Waldo, the no-code testing platform.

How to write automation testing in React Native?

The best thing about creating apps with the React Native app is that it comes with a built-in Jest framework. The Jest testing framework is used for writing automation testing and many other types of tests. In the __tests__ folder, remove the earlier App-test.js file. After that, add two files inside it: ButtonText-test.js and AppText-test.js.

What is the fs module in Node JS?

The promises object of the fs module was introduced in Node.js version 10, so some earlier versions still call the module experimental. This warning was removed when the API became stable in version 12.6. Now that you’ve read a file with the fs module, you will next create a file and write text to it.

How to create a React Native application with NPX?

Go to any directory in the terminal. Use the command npx react-native init <project-name> to create a new project. We will also install the react-native-fs file system package in our application with the below command: We need to first navigate to the folder containing the project in the terminal.


2 Answers

In create-react-app they have stubbed out 'fs'. You cannot import it. They did this because fs is a node core module.
You'll have to find another solution to that problem. See this ticket.

like image 65
kalm42 Avatar answered Oct 11 '22 23:10

kalm42


It's possible this might be an environment issue. It's not possible for the browser to interpret and run some Node server-side modules like fs.

The solution is to run the fs methods in a Node environment (server-side) or to find a package which offers the same functionality but written for the browser.

It's discussed in this question... Module not found: Error: Cannot resolve module 'fs'

And this question... Use fs module in React.js,node.js, webpack, babel,express

like image 4
jess Avatar answered Oct 11 '22 23:10

jess