oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.93k stars 2.75k forks source link

Workers: undefined is not a constructor (evaluating 'new Worker') #3757

Closed mikerockett closed 1 year ago

mikerockett commented 1 year ago

What version of Bun is running?

0.7.0

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

When using Pino, I am unable to use transports – specifically, pino/file, which spawns a Worker as opposed to using the main thread.

  1. bun add pino
  2. Create logger.ts and populate with the below:
import pino from 'pino'
import * as path from 'path'

const transportConfig = pino.transport({
  targets: [
    {
      level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
      target: 'pino/file',
      options: {
        destination: path.resolve(__dirname, '../logs/main.log'),
        mkdir: true
      }
    }
  ]
})

export const logger = pino(transportConfig)

transportConfig.on('ready', () => process.exit(0))
  1. Try log something in entrypoint.ts (logger.info('here')).

What is the expected behavior?

I expect Pino to be able to create a Worker in order for the pino/file target to function.

What do you see instead?

Error thrown:

47 |   const { filename, workerData } = opts
48 |
49 |   const bundlerOverrides = '__bundlerPathsOverrides' in globalThis ? globalThis.__bundlerPathsOverrides : {}
50 |   const toExecute = bundlerOverrides['thread-stream-worker'] || join(__dirname, 'lib', 'worker.js')
51 |
52 |   const worker = new Worker(toExecute, {
                     ^
TypeError: undefined is not a constructor (evaluating 'new Worker')
      at createWorker (/<path>/node_modules/thread-stream/index.js:52:17)
      at new ThreadStream (/<path>/node_modules/thread-stream/index.js:224:18)
      at buildStream (/<path>/node_modules/pino/lib/transport.js:21:17)
      at transport (/<path>/node_modules/pino/lib/transport.js:114:9)
      at /<path>/lib/logger.ts:5:24

Additional information

I'm not sure if this is something Pino needs to fix, however it appears to be trivial and related to Bun still being in the oven…

Jarred-Sumner commented 1 year ago

Duplicate of https://github.com/oven-sh/bun/issues/901