Node

Fix worker_threads module missing

How to fix the error "Cannot find module 'worker_threads'"

Table of Contents
  1. The Problem
  2. The solution

At Coiled we use a few hooks that run with pre-commit. This means that before git commits your changes, these hooks will be run and report any failures. Using pre-commit is a great way to spot issues (like linting) before you do At Coiled we use a few hooks that run with pre-commit. This means that before git commits your changes, these hooks will be run and report any failures. Using pre-commit is a great way to spot issues (like linting) before you do your commits.

Everything went fine when I set up my dev environment, the command pre-commit install throw no errors when I ran it. But on my first commit, I got an error with pyright that apparently couldn't find the worker_threads module.

The Problem

Pyright it's a type checker for Python created by Microsoft. This was the first time I was using pyright so I have never encountered the error.

shell
1pyright..................................................................Failed
2- hook id: pyright
3- exit code: 1
4
5internal/modules/cjs/loader.js:638
6 throw err;
7 ^
8
9Error: Cannot find module 'worker_threads'
10 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
11 at Function.Module._load (internal/modules/cjs/loader.js:562:25)
12 at Module.require (internal/modules/cjs/loader.js:692:17)
13 at require (internal/modules/cjs/helpers.js:25:18)
14 at Object.5013 (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright.js:1:597)
15 at s (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright.js:1:723)
16 at Object.4221 (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright-internal.js:1:204756)
17 at s (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright.js:1:723)
18 at Object.8408 (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright-internal.js:1:158250)
19 at s (/home/fabiorosado/.cache/pre-commit/repov31novgl/node_env-system/lib/node_modules/pyright/dist/pyright.js:1:723)
20
21tslint...............................................(no files to check)Skipped
22tsc..................................................(no files to check)Skipped

This error made me unable to use pre-commit, which is a bummer because I kept forgetting to run black to automatically check and lint my code.

After exploring online, it seems that it might be a node issue, I've tried using Node Version Manager to update node to the latest version (15.3.0), but that didn't work. So, I tried to install the latest node version manually, but that didn't work as well.

The solution

After going through a few StackOverflow questions and Github issues, I came across this GitHub issue created on the electron repository - Unable to use NodeJs worker-threads module.

Running this command will be enough to fix the issue.

shell
1export NODE\_OPTIONS=--experimental-worker

Now I can run git commit, and my pre-commit will run without throwing any errors.

Webmentions

0 Like 0 Comment

You might also like these

How an error in the docs CI made me learn a bit more about RST and how to properly highlight a piece of text.

Read More
Python

Sphinx - How to highlight text

Sphinx - How to highlight text

This article will show you how to setup DynamoDB locally, so you can test your code without having to use your AWS account.

Read More
Databases

How to setup DynamoDB locally

How to setup DynamoDB locally

This article will show you how to create an use indexes in DynamoDB using the python library aioboto3.

Read More
Databases

How to create and use indexes in DynamoDB

How to create and use indexes in DynamoDB

Cheatsheet for LunarVim

Read More
Cheatsheet

LunarVim

LunarVim