简体   繁体   中英

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"
...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM