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

Exploration on how to run Pyscript in a React (NextJS) app, this article explores issues and solutions to run PyScript in a React app.

Read More
PyScript

How to run PyScript in React

How to run PyScript in React

While working on adding tests to Pyscript I came across a use case where I had to check if an example image is always generated the same.

Read More
Python

How to compare two images using NumPy

How to compare two images using NumPy

How to return an attribute from a many-to-many object relationship from a Django Ninja API endpoint.

Read More
Python

Django Ninja Schemas and Many To Many

Django Ninja Schemas and Many To Many