Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected identifier 'parentPort' in worker_threads #27167

Open
wildtang opened this issue Nov 30, 2024 · 1 comment
Open

SyntaxError: Unexpected identifier 'parentPort' in worker_threads #27167

wildtang opened this issue Nov 30, 2024 · 1 comment
Labels
bug Something isn't working correctly node compat

Comments

@wildtang
Copy link

Version: Deno 2.1.2, Windows 10 x64

I wrote a program using Node.js, TypeScript and a lib called microjob. the program code below:

export class EnvChecker {
  static async checkRuntimes() {

    // 首先通过进程命令行获取运行时类型(node/tsx/deno/bun...)
    const willCheck = process.env.EC_ON_LAUNCH || false
    if (willCheck) {
      try {
        // start the worker pool first
        await start({ maxWorkers: 1 })

        // this function will be executed in another thread
        const res = await job(() => {
          console.log(`run time is: ${process.argv[0]}`)
          // doing some other tasks, skip here
          return 'Env check done.'
        })
        console.log(res)
      } catch (err) {
        console.error(err)
      } finally {
        await stop() // shutdown worker pool
      }
    }
  }
}

EnvChecker.checkRuntimes()

When I execute it use deno, got error:

PS F:\my-program> npm run dev:deno

> deno run  --watch  --inspect  --allow-all      src/main.ts

Watcher Process started.
Debugger listening on ws://127.0.0.1:9229/ws/3426a06b-7d14-4a71-bdca-e45de5e07894
Visit chrome://inspect to connect to the debugger.
Debugger listening on ws://127.0.0.1:9229/ws/0ef0d61e-7b4c-4b57-94bc-2633e8907f82
Visit chrome://inspect to connect to the debugger.
error: Uncaught (in worker "[worker eval]") SyntaxError: Unexpected identifier 'parentPort'
    at <anonymous> (data:text/javascript,const { parentPort }......error(err)    }  }}):1:49)
2024/11/30 13:16:01 Web app  > Main Process launched, name: Mot&Mot
2024/11/30 13:16:01 Web app  > Server is running on port: 55501
2024/11/30 13:16:01 Web app  > Process(Whole App/Service) instance xid(random): ct59v4cn97dj54118l2g, OS process id: 12944
Debugger listening on ws://127.0.0.1:9229/ws/955a666c-c547-46c9-a6f8-7fea6490b9d5
Visit chrome://inspect to connect to the debugger.
error: Uncaught (in worker "[worker eval]") SyntaxError: Unexpected identifier 'parentPort'
    at <anonymous> (data:text/javascript,const { parentPort }......error(err)    }  }}):1:49)
[Object: null prototype] {
  message: "Uncaught SyntaxError: Unexpected identifier 'parentPort'",
  fileName: "data:text/javascript,const { parentPort } = require('worker_threads')parentPort.on('message', async worker => {  const response = {    error: null,    data: null  }  try {    eval(worker)    // __executor__ is defined in worker    response.data = await __executor__()    parentPort.postMessage(response)  } catch (err) {    response.data = null    response.error = {      message: err.message,      stack: err.stack    }    try {      parentPort.postMessage(response)    } catch (err) {      console.error(err)    }  }})", 
  lineNumber: 1,
  columnNumber: 49
}

Meanwhile, this program works fine in node/tsx and bun:

PS F:\my-program> npm run dev:tsx 
> npx   tsx    watch  --inspect  --env-file=.env  src/main.ts

Debugger listening on ws://127.0.0.1:9229/085d4c46-186c-43c4-9676-95fc522e81c4
For help, see: https://nodejs.org/en/docs/inspector
run time is: C:\Apps\Nvm\Node\node.exe
Env check done.
PS F:\my-program> npm run dev:bun
> bun   run  --watch  --inspect                   src/main.ts

--------------------- Bun Inspector ---------------------
Listening:
  ws://localhost:6499/m4h84jh099g
Inspect in browser:
  https://debug.bun.sh/#localhost:6499/m4h84jh099g
--------------------- Bun Inspector ---------------------
run time is: C:\Users\jason\scoop\apps\bun\1.1.38\bun.exe
Env check done.
@marvinhagemeister marvinhagemeister added bug Something isn't working correctly node compat labels Dec 2, 2024
@marvinhagemeister marvinhagemeister changed the title When running thread pool, got error: Uncaught (in worker "[worker eval]") SyntaxError: Unexpected identifier 'parentPort' Node worker_threads: SyntaxError: Unexpected identifier 'parentPort' Dec 2, 2024
@marvinhagemeister marvinhagemeister changed the title Node worker_threads: SyntaxError: Unexpected identifier 'parentPort' SyntaxError: Unexpected identifier 'parentPort' in worker_threads Dec 2, 2024
@marvinhagemeister
Copy link
Contributor

This issue is actually two issues.

  1. Syntax error, fixed by fix(node/worker_threads): data url not encoded properly with eval #27184
  2. eval code loaded as ESM instead of CJS, node:worker_threads doesn't load passed in text as CommonJS #27181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly node compat
Projects
None yet
Development

No branches or pull requests

2 participants