728x90
반응형

export 'default' (imported as 'XLSX') was not found in 'xlsx' 에러 원인

import XLSX from 'xlsx';

// 중략...
const handleFileUpload = (event) => {
    const file = event.target.files[0];
    const reader = new FileReader();

   reader.onload = (e) => {
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, { type: 'array' });

      // 첫 번째 시트를 가져옴
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];

      // 셀 데이터를 파싱하여 출력
      const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
      console.log(jsonData);
    };

    reader.readAsArrayBuffer(file);
  };
  
  ...
}

최신 버전의 xlsx 라이브러리에서는 기본 내보내기(default export)가 제공 X

named export를 사용해야 함

 


해결 방법

named export를 사용하기

전체(*) name 가져오기

import * as XLSX from 'xlsx';

 

사용할 name만 가져오기

XLSX.read => read, XLSX.utils => utils로 변경해서 코드 작성하기

import { read, utils } from 'xlsx';

// 중략...
const handleFileUpload = (event) => {
    const file = event.target.files[0];
    const reader = new FileReader();

   reader.onload = (e) => {
      const data = new Uint8Array(e.target.result);
      const workbook = read(data, { type: 'array' });

      // 첫 번째 시트를 가져옴
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];

      // 셀 데이터를 파싱하여 출력
      const jsonData = utils.sheet_to_json(worksheet, { header: 1 });
      console.log(jsonData);
    };

    reader.readAsArrayBuffer(file);
  };
  
  ...
}
반응형
복사했습니다!