I am using NestJS/TypeScript and one of my service files has the following imports which were taken from their (SheetJS) documentation page.
/* load the codepage support library for extended support with older formats */
import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
import * as XLSX from 'xlsx/xlsx.mjs';
/* load 'fs' for readFile and writeFile support */
import { Injectable } from '@nestjs/common';
import { DSRequestContextService } from '@shared/services/context/context_service';
import { DSLogService } from '@shared/services/log/log.service';
XLSX.set_fs(fs);
XLSX.stream.set_readable(Readable);
XLSX.set_cptable(cpexcel);
However I get the following error:
[ERROR] 18:11:27 Error: require() of ES Module /mnt/d/Repos/document-engine/node_modules/xlsx/dist/cpexcel.full.mjs not supported. Instead change the require of /mnt/d/Repos/document-engine/node_modules/xlsx/dist/cpexcel.full.mjs to a dynamic import() which is available in all CommonJS modules.
Anyone have any ideas how to fix this?
since your project is using CJS, you should use the
import()
expressions to load ESM-only modules, as described in NodeJS docs: https://nodejs.org/api/esm.html#import-expressions