反应Js需要'fs'

joe*_*joe 7 fs reactjs mashape webpack

我有

import fs from 'fs'
Run Code Online (Sandbox Code Playgroud)

在我的package.json我有

然后我运行命令

>  npm i fs
>  fs@0.0.2 node_modules/fs
Run Code Online (Sandbox Code Playgroud)

接下来在我的React商店中导入'fs'模块

但是当我尝试使用fs时

我没有看到除了构造函数和一些其他__methods之外的方法.我没有看到方法createReadStream或任何其他文件操作方法.

有人知道出了什么问题吗?(使用Webpack)并可根据要求提供更多信息,但我到目前为止......

ps:为什么我可以npm i fs - 当我在其他帖子上阅读时,我不必这样做(使用节点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
  },
  .
  .
  .
  .
})
Run Code Online (Sandbox Code Playgroud)

kal*_*m42 6

create-react-app他们已经删除'fs'.你无法导入它.他们这样做fs是因为它是一个节点核心模块.
你必须找到解决这个问题的另一种方法.看到这张票.


Jes*_*s B 5

这可能是环境问题。浏览器不可能解释和运行一些 Node 服务器端模块,比如fs.

解决方案是fs在 Node 环境(服务器端)中运行这些方法,或者找到一个提供相同功能但为浏览器编写的包。

在这个问题中讨论了...... 找不到模块:错误:无法解析模块'fs'

还有这个问题... 在 React.js、node.js、webpack、babel、express 中使用 fs 模块