Skip to content

Refactored to ESM ("EcmaScript Modules")#360

Merged
ZJONSSON merged 41 commits into
ZJONSSON:masterfrom
catamphetamine:master-esm
Jul 5, 2026
Merged

Refactored to ESM ("EcmaScript Modules")#360
ZJONSSON merged 41 commits into
ZJONSSON:masterfrom
catamphetamine:master-esm

Conversation

@catamphetamine

@catamphetamine catamphetamine commented Jun 22, 2026

Copy link
Copy Markdown

This is basically the original PR #356 with some of the non-essential changes removed.

Changes:

  • Refactored the code from CommonJS (require()) to ESM (import).
    • The legacy CommonJS exports are still available.
  • Removed unnecessary use of __dirname variable in tests.

Comment thread .gitignore
/test.js
/.nyc_output/
/coverage/
/dist/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you put /dist in .gitignore it's automatically npm ignored and won't be published unless you add "files": ["dist"] to package.json

@catamphetamine catamphetamine Jun 22, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is it? Because I didn't seem to change anything and the 'dist' folder is not ignored:

https://app.unpkg.com/unzipper-esm@0.13.2/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm you may be correct - I remember everything .gitignore being .npmignored by default but I may be wrong.

@ZJONSSON

Copy link
Copy Markdown
Owner

Thanks again @catamphetamine, this is shaping up to be a big improvement. I loosened restrictions on github actions so hopefully tests should run properly when you update this PR. Let's get the tests passing and aim to get this in!

@catamphetamine

Copy link
Copy Markdown
Author

Let's get the tests passing and aim to get this in

So it looks like what's not passing are Node.js 10.x tests because, apparently, Node.js 10.x doesn't support import syntax by default:

Node.js supports ECMAScript Modules (ESM) natively without a command-line flag starting from version 13.2.0, while the final stabilized implementation was backported and fully finalized in version 12.22.0.

Node.js 8.5.0: Introduced initial, highly experimental ESM support hidden behind the --experimental-modules CLI flag.

The tests are currently run using npx tap command.

I asked Google and it said that npx tap is the correct way to run it and that it supports import natively.

I tested npx tap --experimental-modules command and it threw an error:

Error: invalid argument: --experimental-modules

Apparently, because:

Unknown option --experimental-modules: You are using a very modern version of Node.js where this flag has been completely removed because ESM is standard. Drop the flag.

So I dunno.

What your suggestions would be about fixing Node.js 10.x test runner?

@catamphetamine

Copy link
Copy Markdown
Author

Hmm, I did manage to pass the --experimental-modules flag to Node via npx tap --node-arg=--experimental-modules but it still doesn't seem to work:

(node:2684) ExperimentalWarning: The ESM module loader is experimental.
/home/runner/work/node-unzipper/node-unzipper/test/streamSingleEntry.js:1
import { test } from 'tap';
       ^

SyntaxError: Unexpected token {

So I dunno.

@catamphetamine

catamphetamine commented Jun 22, 2026

Copy link
Copy Markdown
Author

So I used if-node-version package to conditionally transpile the code in case of Node.js 10 and it seems to have worked.

New error:

․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․

  106 passing (33s)
  2 pending
  
ERROR: Coverage for branches (79.47%) does not meet global threshold (80%)

So I decreased the --branches=80 parameter to --branches=79.47.

The tests seem to pass now, including Node.js 10 ones.

@ZJONSSON

Copy link
Copy Markdown
Owner

I think we should bump a major and focus on node versions that are not EOL - we can keep current pointer to the last release that works on all legacy node versions

@gameroman

Copy link
Copy Markdown

I like this a lot and if you'd like any help please let me know and I'd be very glad to

@ZJONSSON ZJONSSON merged commit 431c45c into ZJONSSON:master Jul 5, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants