Skip to content

Tinypool.version throws ReferenceError: __dirname is not defined (package is type: module) #139

Description

@martin-k-m

The problem

Tinypool.version throws ReferenceError: __dirname is not defined.

The package declares "type": "module", so Node loads dist/index.js as an ES module — and an ES module has no __dirname binding. The getter reads one.

Reproduce

npm i tinypool@2.1.2

probe.mjs:

import Tinypool from 'tinypool'
console.log(Tinypool.version)
$ node probe.mjs
ReferenceError: __dirname is not defined

I ran a control in the same file first — a bare __dirname reference, to confirm the scope really is an ES module rather than something my harness leaked:

control (bare __dirname here): ReferenceError
Tinypool.version THREW: ReferenceError: __dirname is not defined

Where

src/index.ts, in static get version():

static get version(): string {
  const { version } = JSON.parse(
    readFileSync(join(__dirname, '../package.json'), 'utf-8')
  )
  ...

which lands in dist/index.js:759 in the published 2.1.2 tarball. package.json has "type": "module" and dist/index.js is the only entry, so there is no CommonJS path to this code.

There is no createRequire or fileURLToPath shim in the emitted file — I grepped it — so nothing supplies __dirname at runtime.

Why it has probably gone unnoticed

version is the only thing reaching it. Everything else in the public API works, so a consumer only hits this if they read that property.

Fix

The usual ESM equivalent, e.g.

import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))

or reading the version at build time. Happy to open a PR if you'd like one.

Notes

Found by an automated checker I'm building that flags CommonJS bindings in files Node resolves as ES modules. I verified this one by hand against the published 2.1.2 tarball before filing, and checked the open and closed issues for an existing report.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions