React Js require 'fs'

。_饼干妹妹 提交于 2019-12-21 12:43:05

问题


I have

import fs from 'fs'

and in my package.json I have

Then I run the command

>  npm i fs
>  fs@0.0.2 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
  },
  .
  .
  .
  .
})

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/35003961/react-js-require-fs

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