Linter error - Browser-only function is not defined

可紊 提交于 2019-12-11 03:08:55

问题


I'm relying on a class that is available in browser, FileReader

I keep getting an error in webpack - 'FileReader' is not defined no-undef

What is the correct way of dealing with this? I'm currently using a method where I just ignore the message.


回答1:


The issue is that since webpack cannot find it as part of Node.js and since it is not available, it will cause an error. But there are a few ways of getting around this.

Instead of

var reader = new FileReader();

use

Fix #1

var reader = new window.FileReader();

Fix #2

var reader = new global.FileReader();

webpack, by default, will convert global to window. More info at: https://webpack.js.org/configuration/node/

Fix #3

// in webpack.config.js
module.exports = {
  //...
  externals: {
    FileReader: 'FileReader'
  }
};

more info: https://webpack.js.org/configuration/externals/



来源:https://stackoverflow.com/questions/40871095/linter-error-browser-only-function-is-not-defined

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