andywer / andywer/threads.js

TypeError: Cannot read property 'resolve' of null

オープン
#376 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
3.5k
フォーク
173
PR マージ指標
30日以内にマージされた PR はありません

説明

Hi,
Thank you for your library. I'd like to use it on my electron app (not using electron webpack) but I cannot get it working.

Here is the first stacktrace:
```log
Top-level unhandled promise rejection: TypeError: Cannot read property 'resolve' of null
at resolveScriptPath (/home//pro/contribs/loki-messenger/node_modules/threads/dist/master/implementation.node.js:84:26)
at new Worker (/home//pro/contribs/loki-messenger/node_modules/threads/dist/master/implementation.node.js:160:23)
at Object.doWhatYouGottaDo (/home//pro/contribs/loki-messenger/ts/workers/master.js:7:40)
[...]
```

I have my `ts` files under `/ts`
I have the worker files under `/ts/workers`

Under `/ts/workers` I have
`master.ts` and `auth.ts`

`auth.ts` :
```ts
// workers/auth.js - will be run in worker thread
// tslint:disable-next-line: no-submodule-imports
import { expose } from 'threads/worker';

expose({
hashPassword(password, salt) {
return `${password}:${salt}`;
},
});

```

`master.ts` :
```ts
import { spawn, Thread, Worker } from 'threads';

export async function doWhatYouGottaDo() {
const auth = await spawn(new Worker('./auth'));
// tslint:disable-next-line: await-promise
const hashed = await auth.hashPassword('Super secret password', '1234');
console.log('Hashed password:', hashed);
await Thread.terminate(auth);
}

doWhatYouGottaDo().catch(console.error);
```

Tthe issue comes from the require() on this line
`eval("require").resolve(makeRelative(rebaseScriptPath(scriptPath, /[\/\\]worker_threads[\/\\]/)));`

it's simply the 'require' which is not found (eval("require") = null).

I assume it comes from the tsconfig.json but I don't find what to set.
Here is my current `tsconfig.json`

```json
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es2017"
],
"jsx": "react",
"sourceMap": true,
"rootDir": "./ts",

"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
"esModuleInterop": true
}
}
```

I think I need to change the module "commonjs" to "esnext" as you say in the [guide](https://threads.js.org/getting-started#when-using-typescript) but then my whole app fails to start with errors like `SyntaxError: Cannot use import statement outside a module`.

I thought I could just override the tsconfig.json for the workers path it doesn't seem to be picked up by tsc. I tried to add a tsconfig.json in `ts/workers` containing

```json
{
"extends": "../../tsconfig.json",

"compilerOptions": {
"module": "esnext"
}
}
```

But I still get the same `TypeError: Cannot read property 'resolve' of null `

Do you have any idea what I can do to get this working on an electron app relying on tsc and not electron-webpack?
Thanks a lot

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。