Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linter error - Browser-only function is not defined

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.

like image 780
Daniel Avatar asked Nov 29 '16 16:11

Daniel


People also ask

How do you fix function is not defined?

To clear a text input, assign blank string to 'value' attribute (not innerHTML attribute) document . getElementById("text"). value = ""; And don't call myfunction in JS tab If you use a <form> then an input with 'type' attribute "reset" will do the same thing.

Why is function not defined?

Answer and Explanation: A function is not defined or is undefined if the value to be inputted is not in its domain.

What does it mean when a function is not defined in JavaScript?

The JavaScript exception "variable is not defined" occurs when there is a non-existent variable referenced somewhere.

How do you fix the error require is not defined in node JS?

The module type is used to make Node treat . js files as ES modules. Instead of require() , you need to use the import/export syntax. To solve the issue, remove the "type": "module" from your package.


1 Answers

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/

like image 184
Daniel Avatar answered Sep 25 '22 00:09

Daniel