TypeError: Cannot read properties of undefined (reading 'utils') at read_utf16 #2898

Closed
opened 2023-03-09 16:36:08 +00:00 by brismuth · 4 comments

I'm using xlsx version 0.18.5.

My application can read all CSVs except this one, and I can't figure out what's special about it. I received a CSV similar to this one from a customer who is using my application, and I was able to simplify it down to the current state and still reproduce the isse. I'm wondering if there's some kind of invalid header on the CSV, but I'm not sure how to check that.

This CSV causes XLSX to throw the error TypeError: Cannot read properties of undefined (reading 'utils').

I'm using react, and importing sheetjs like this:

import { read } from "xlsx";

and here's how I'm using it:

    const data = await file.arrayBuffer();
    try {
      let newWorkbook = read(data, {codepage: 65001});
      setWorkbook(newWorkbook);
    } catch (err) {
      setErrorMessage((<div>Error parsing CSV. <br/><br/>{err.stack}</div>));
      setErrorVisible(true);
    }

Here's the error stack:

TypeError: Cannot read properties of undefined (reading 'utils') at read_utf16
(http://localhost:3000/static/js/bundle.js:216764:16) at readSync
(http://localhost:3000/static/js/bundle.js:216865:16) at readSync 
(http://localhost:3000/static/js/bundle.js:216795:81) at uploadCSV 
(http://localhost:3000/main.7a415aa189e87966af90.hot-update.js:182:68)

I think the error is coming from this line:

https://github.com/SheetJS/sheetjs/blob/master/bits/87_read.js#L53

I'm using `xlsx` version 0.18.5. My application can read all CSVs except this one, and I can't figure out what's special about it. I received a CSV similar to this one from a customer who is using my application, and I was able to simplify it down to the current state and still reproduce the isse. I'm wondering if there's some kind of invalid header on the CSV, but I'm not sure how to check that. This CSV causes XLSX to throw the error `TypeError: Cannot read properties of undefined (reading 'utils')`. I'm using react, and importing sheetjs like this: ```js import { read } from "xlsx"; ``` and here's how I'm using it: ```js const data = await file.arrayBuffer(); try { let newWorkbook = read(data, {codepage: 65001}); setWorkbook(newWorkbook); } catch (err) { setErrorMessage((<div>Error parsing CSV. <br/><br/>{err.stack}</div>)); setErrorVisible(true); } ``` Here's the error stack: ``` TypeError: Cannot read properties of undefined (reading 'utils') at read_utf16 (http://localhost:3000/static/js/bundle.js:216764:16) at readSync (http://localhost:3000/static/js/bundle.js:216865:16) at readSync (http://localhost:3000/static/js/bundle.js:216795:81) at uploadCSV (http://localhost:3000/main.7a415aa189e87966af90.hot-update.js:182:68) ``` I think the error is coming from this line: https://github.com/SheetJS/sheetjs/blob/master/bits/87_read.js#L53
Owner

Thanks for the report! So we can reproduce locally, are you loading the encoding support as described in https://docs.sheetjs.com/docs/getting-started/installation/frameworks#encoding-support ? (in general, forcing UTF8 support should not require the codepage encoding library)

Thanks for the report! So we can reproduce locally, are you loading the encoding support as described in https://docs.sheetjs.com/docs/getting-started/installation/frameworks#encoding-support ? (in general, forcing UTF8 support should not require the codepage encoding library)
Author

Hmm, no, I didn't see that section in the docs, so I'm just loading in xlsx as described in my post.

I can try loading in the full codepage encoding library and see if that resolves the issue.

Hmm, no, I didn't see that section in the docs, so I'm just loading in `xlsx` as described in my post. I can try loading in the full codepage encoding library and see if that resolves the issue.
Owner

We'll take a closer look later today, but that specific error suggests that the internal codepage instance is defined but not shaped as expected. It is loaded by default when using CommonJS (nodejs require) but not loaded when using the ESM build.

We'll take a closer look later today, but that specific error suggests that the internal codepage instance is defined but not shaped as expected. It is loaded by default when using CommonJS (nodejs require) but not loaded when using the ESM build.
Author

I confirmed that adding

import { set_cptable } from "xlsx";
import * as cptable from 'xlsx/dist/cpexcel.full.mjs';
set_cptable(cptable);

Resolved the issue with this particular CSV. Thanks for the pointer!

I confirmed that adding ```js import { set_cptable } from "xlsx"; import * as cptable from 'xlsx/dist/cpexcel.full.mjs'; set_cptable(cptable); ``` Resolved the issue with this particular CSV. Thanks for the pointer!
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2898
No description provided.