catamphetamine / read-excel-file

Read *.xlsx files in a browser or Node.js. Parse to JSON with a strict schema.

Home Page:https://catamphetamine.gitlab.io/read-excel-file/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node.js ESM not working (missing imports, types)

peterhirn opened this issue · comments

This package is not usable when running Node.js in ESM mode (edit: in combination with pnpm or yarn), eg. adding "type": "module" to package.json.

import readXlsxFile from "read-excel-file/node"
> node src/index.js

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '...\node_modules\.pnpm\read-excel-file@5.6.0\node_modules\@xmldom\xmldom\' imported from ...\node_modules\.pnpm\read-excel-file@5.6.0\node_modules\read-excel-file\modules\xml\xml.js
Did you mean to import @xmldom+xmldom@0.8.6/node_modules/@xmldom/xmldom/lib/index.js?

Furthermore the typescript definitions are broken in ESM and there is a missing dependency on @types/node:

> tsc --build

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:4:26 - error TS2307: Cannot find module 'fs' or its corresponding type declarations.

4 import { PathLike } from 'fs';
                           ~~~~

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:5:24 - error TS2307: Cannot find module 'stream' or its corresponding type declarations.

5 import { Stream } from 'stream';
                         ~~~~~~~~

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:13:8 - error TS2307: Cannot find module '../types.d' or its corresponding type declarations.

13 } from '../types.d';
          ~~~~~~~~~~~~

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:21:8 - error TS2307: Cannot find module '../types.d' or its corresponding type declarations.

21 } from '../types.d';
          ~~~~~~~~~~~~

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:25:23 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

25 type Input = Stream | Buffer | PathLike;

This package is not usable when running Node.js in ESM mode, eg. adding "type": "module" to package.json.

The error doesn't really indicate of any issues with ESM mode specifically.
More like issue in @xmldom/xmldom package itself, or in your node_modules.
Regardless of using type: "module" flag, both ESM and non-ESM dependencies are supported.

Furthermore the typescript definitions are broken in ESM and there is a missing dependency on @types/node

I'm personally not a TypeScript expert, so if you know how the issue could be fixed, you can post your suggestions.

"type": "module" works for you?

Can you take a look at my minimal test repo https://github.com/peterhirn/read-excel-file-esm

I'm on Node 19.5, Windows 10.

Could also be related to pnpm.

I think it's best to ignore the TypeScript issues for now.

"type": "module" works for you?

I haven't really used this library in any of such projects.

Can you take a look at my minimal test repo https://github.com/peterhirn/read-excel-file-esm

Works on my machine:

image

Thanks for testing it!

The problem is pnpm / yarn in combination with ESM. npm works fine. I'm trying to figure out what's going on here.

Edit: Never mind, after doing a fresh checkout I can't reproduce this anymore. Must have been related to node_modules, as you suggested earlier. I'll look into the typescript issue and reopen or create a pull request if it makes sense. Thank you for your time.