【问题标题】:PSD.js in react error fs.readFileSync is not a function反应错误 fs.readFileSync 中的 PSD.js 不是函数
【发布时间】:2017-02-14 10:28:02
【问题描述】:

我有一个使用 psd.js 的 reactjs 项目,并且在我的 dropzone 配置上像这样

accept: function(file, done){
        const reader = new FileReader();
        reader.onload = handleReaderLoad;
        reader.readAsDataURL(file);
        function handleReaderLoad(evt) {
          console.log(evt.target.result);
          let psdFile = PSD.fromFile(evt.target.result);
          psdFile.parse();
          console.log(psdFile);
        }
        done();
      },

错误是:

Uncaught TypeError: fs.readFileSync is not a function
    at Function.fromFile (init.coffee:6)
    at FileReader.handleReaderLoad (index.js?03a7:153)

在我的 webpack 配置中我包括:

node: {
    fs: 'empty'
  },

因为如果我不包含它,错误是:not found fs module

请帮忙。

【问题讨论】:

    标签: javascript reactjs webpack psd


    【解决方案1】:

    您应该使用PSD.fromEvent(evt),而不是PSD.fromFile

    前者从文件输入中读取 blob,而后者尝试访问浏览器上下文中显然不存在的文件系统。

    所以我猜你的代码应该是这样的(但我不太确定)

    accept: function(file, done){
        const reader = new FileReader();
        reader.onload = handleReaderLoad;
        reader.readAsDataURL(file);
        function handleReaderLoad(evt) {
          PSD.fromEvent(evt).then(function (psd) {
               // here you can access the parsed file as psd
               console.log(psd.tree().export());
              done();
          });
        }
    
      },
    

    【讨论】:

    • 如果我使用PSD.fromEvent() 错误是Uncaught TypeError: PSD.fromEvent is not a function at FileReader.handleReaderLoad。怎么了@pawel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2018-09-24
    • 2020-11-04
    • 1970-01-01
    • 2021-11-30
    • 2018-01-10
    相关资源
    最近更新 更多