首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React Js需要“fs”

React Js需要“fs”
EN

Stack Overflow用户
提问于 2016-01-26 06:48:48
回答 3查看 73.3K关注 0票数 21

我有过

代码语言:javascript
运行
复制
import fs from 'fs'

在我的package.json里我有

然后我运行该命令

代码语言:javascript
运行
复制
>  npm i fs
>  fs@0.0.2 node_modules/fs

接下来,在我的React store中,我导入了'fs‘模块

代码语言:javascript
运行
复制
import fs from 'fs'

但是,当我尝试使用fs时

除了构造器和其他一些__methods之外,我看不到任何方法。我没有看到createReadStream方法或任何其他文件操作方法。

有人知道哪里出了问题吗?(使用Webpack),并可以根据要求提供更多信息,但我已经走到这里了……

ps:为什么我可以npm i fs --当我在其他帖子上读到我不需要这样做时,我可以保存(使用节点5.5.0)

代码语言:javascript
运行
复制
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
  },
  .
  .
  .
  .
})

EN

回答 3

Stack Overflow用户

发布于 2018-02-19 04:11:28

create-react-app中,他们去掉了'fs‘。您不能导入它。他们这样做是因为fs是一个节点核心模块。

你必须找到另一个解决方案来解决这个问题。请参阅this ticket.

票数 20
EN

Stack Overflow用户

发布于 2018-01-21 03:44:35

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

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

这个问题已经讨论过了.Module not found: Error: Cannot resolve module 'fs'

这个问题..。Use fs module in React.js,node.js, webpack, babel,express

票数 4
EN

Stack Overflow用户

发布于 2022-01-07 08:09:37

npm i babel-plugin-preval

您可以在React中使用 ,如果您想在React中使用节点的fs,如下所示

代码语言:javascript
运行
复制
const greetingContent = preval`
  const fs = require('fs')
  module.exports = fs.readFileSync(require.resolve('./greeting.txt'), 'utf8')
`
console.log(greetingContent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35003961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档