Typescript cannot find module 'worker_threads'
Asked Answered
I

3

9

I am trying to use node.js 'worker_threads' library with Node v14.15.1 and getting this error

Cannot find module 'worker_threads' or its corresponding type declarations.ts(2307)
tsc 

src/api/services/email.service.ts:1:62 - error TS2307: Cannot find module 'worker_threads'.

1 import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
                                                              

Found 1 error.

my code -

import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';

worker threads got stable after node 11. So it should work. What am i doing wrong ?

PS - I ran same code with JS and it worked fine so i don't think its Node.js issue. Other core libraries like fs are also working fine. Do I need any additional config for TS ?

Impregnable answered 19/12, 2020 at 15:39 Comment(6)
npm install @types/node or the equivalent command for your package manager of choice.Pye
Thanks but already done that and other core libraries like 'fs' are working fine. Its just with this libraryImpregnable
Verify that the version of the @types package is correctPye
@AluanHaddad thanks for your help. I don't remember why, but I was having very old version of @types/node and ts-node. After updating it worked great. Thanks again!Impregnable
@RohanGulati I have exactly the same problem, what versions of @types/node and ts-node did you update to? Would be helpful if you could specify the steps as an answer or in comments.Monohydroxy
@Monohydroxy thanks for reminding. I have added answerImpregnable
I
7

This issue was that I was using quite old versions. After updating, the issue was resolved. If someone faces same issue, update these versions in your package.json - I have updated to

  • "@types/node": "14.14.14"
  • "ts-node": "9.1.1"

Also make sure Nodejs version is 12 LTS or above to have stable 'worker-thread' support.

Impregnable answered 26/12, 2020 at 12:14 Comment(2)
It doesn't help for "ts-node": "^9.1.1" and "@types/node": "^14.14.37".Bennink
@EugeneZalivadnyi This is well tested with these versions. What problem are you facing ? May be I can helpImpregnable
M
2

In my case, this problem happened in my Angular project after npm audit fix --force

So after few attempts here's what I did:

  • updating node with sudo n stable (you should install Node’s version manager first)

  • ng update

This command will give you few recommendations in a column named Command to update run each separately and make sure it worked correctly.

Then I run ng serve and everything got back to normal.

Moralez answered 9/6, 2021 at 13:48 Comment(0)
N
0

I got the same error on ubuntu. I recently installed node on my new machine. I had been on Windows before. I realized that I was using a much older version of node (v10 or similar).

After upgrading the node version to the latest v16.13.1, I was able to resolve the error. I also had to delete the node_modules folder and package-lock.json file. I took Rohan's suggestion above and followed the instructions provided here:

https://github.com/enable3d/enable3d/issues/231

Nummular answered 20/12, 2021 at 0:22 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Aw

© 2022 - 2024 — McMap. All rights reserved.