I had a really weird problem today.

An Excel import function, adopt the method of dynamic loading JS load XLSX script, run in the browser is normal, but the page open embedded Electron, have met the program error XLSX. Read is not a function.

Even weirder, this project has developed similar features and released them online before with no problems. Therefore, I continued to investigate and found that this error was reported only in the Electron local debugging environment. There was no problem in the formal Electron environment and the browser. Similar functions were developed before, but they just happened not to be debugged in the Electron local debugging environment.

An issue XLSX. Read is not a function Electron JS · Issue #2209 · SheetJS/ SheetJS was found on Github of XLSX, but it doesn’t solve the problem.

Download the source code for XLSX and find a code that says that XLSX determines the parameters passed to make_xlsx_lib based on the environment.

Break on xlsx.min.js to see how the arguments to make_xlsx_lib differ

The breakpoints in the Electron local debug environment are as follows:

The official environment breakpoint of Electron is as follows

Debug environment Electron, open Console, check module, find not undefined

In the formal environment Electron, open Console, check module, find undefined

At this point, the problem is identified as XLSX itself causing the problem, and has no impact in the formal environment.

So why does XLSX do this? Is XLSX doing something wrong? Actually it’s not XLSX fault, but the UMD specification, many libraries have a similar problem such as jQuery stackoverflow.com/questions/3…

The difference between the Electron local debug environment mentioned above and the formal environment is whether nodeIntegration is enabled. When nodeIntegration is enabled, the Module is not empty and XLSX will assume that it is in the NodeJS environment, so it adopts the CommonJS specification.

This question is also mentioned in the Electron FAQ.

Because Electron introduces Node.js into the runtime environment, there are a few extra variables in the DOM, such as Module, exports, and require. This causes many libraries to fail because they also need to add variables of the same name to the runtime environment. This can be resolved by disabling nodeIntegration