Error in TypeScript + ESM setup
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 261
- Forks
- 31
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 4
Description
Hi,
I'm using thread-stream (through pino) in a project that uses TypeScript compiled to ESM modules, running in Node.js.
I'm getting this error when running the project:
file:///path-to-project/node_modules/thread-stream/index.js:50
const toExecute = bundlerOverrides['thread-stream-worker'] || join(__dirname, 'lib', 'worker.js')
^
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/path-to-project/packages/repco-cli/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at createWorker (file:///path-to-project/node_modules/thread-stream/index.js:50:70)
at new ThreadStream (file:///path-to-project/node_modules/thread-stream/index.js:224:19)
at buildStream (file:///path-to-project/node_modules/pino/lib/transport.js:21:18)
at transport (file:///path-to-project/node_modules/pino/lib/transport.js:110:10)
at normalizeArgs (file:///path-to-project/node_modules/pino/lib/tools.js:297:16)
at pino (file:///path-to-project/node_modules/pino/pino.js:86:28)
at file:///path-to-project/packages/repco-common/src/log.ts:29:20
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at ESMLoader.import (node:internal/modules/esm/loader:526:24)
I could fix the error through a hackish solution for the time being:
import module from 'module'
import p from 'path'
import pino from 'pino'
// Fix thread-stream error due to __dirname
// @ts-ignore
globalThis.__bundlerPathsOverrides = {
'thread-stream-worker': p.join(
p.dirname(module.createRequire(import.meta.url).resolve('thread-stream')),
'lib',
'worker.js',
),
}
const logger = pino()
However, it would be nice if that could be fixed somehow upstream.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at node_modules/thread-stream/index.js around createWorker, especially the __dirname reference at line 50, and inspect the lib/worker.js path it selects. Reproduce the reported TypeScript-compiled ESM setup through pino; done means the project runs without the __dirname ReferenceError and no consumer-side global override is required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, typescript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100