Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import `xlsx` in a node.js EcmaScript module wrote in typescript?

Using Node.js v18.12.1, I'm trying to use https://www.npmjs.com/package/xlsx in a node.js EcmaScript module wrote in typescript.

However, when I follow the instructions for Node.js (https://docs.sheetjs.com/docs/getting-started/installation/nodejs#esm-import) like this :

import * as fs from 'fs';

import * as XLSX from 'xlsx';
XLSX.set_fs(fs);

I get the following runtime (not compile time) error: TypeError: XLSX.set_fs is not a function

What is wrong with this import ?

tsconfig.json:

...
   "target": "ESNext", 
   "module": "ESNext",
...

package.json:

...
"type": "module"
...
like image 563
Victor Avatar asked Oct 20 '25 04:10

Victor


1 Answers

Older releases are technically available on the public npm registry as xlsx, but the registry is out of date. The latest version on that registry is 0.18.5

Following these steps solves the problem : https://docs.sheetjs.com/docs/getting-started/installation/nodejs#installation

like image 127
Victor Avatar answered Oct 22 '25 17:10

Victor