Aral Balkan

Mastodon icon RSS feed icon P2P network icon

Workaround for npm install error on Lineageos 15.1

Screenshot of Emacs showing the fix outlined in this post being applied.

The fix.

npm install fails in Termux on LineageOS 15.11 with the following error:

NPM ERR! Cannot read property 'length' of undefined

The issue originates from a bug in node. The affected module has implemented a workaround that you can manually apply to your npm installation to avoid the issue until the upstreams fix it properly.

To implement the workaround:

  1. Open the file you need to patch in your editor2:

    $EDITOR $PREFIX/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js
  2. Update line 5:


    , maxConcurrentWorkers : require('os').cpus().length


    , maxConcurrentWorkers : (require('os').cpus() || { length: 1 }).length
  3. Replace the 1 in the code snippet above with the number of cores that your phone has and save the file. (For my Samsung S9+, I used 8 as it has an octa-core processor.)

That should fix the problem and you should be able to use npm again.

  1. I did not encounter this error on my Nexus 5 with LineageOS 14.1. The phone exhibiting the error is running LineageOS 15.1, Termux version 0.64, Node.js version 8.11.3 and npm version 5.6.0. [return]
  2. If you get a command not found error at this step, it’s most likely because you haven’t specified a default editor to use in your shell configuration. To fix that and use a simple editor called nano as your default, execute the following command: echo "export EDITOR='nano'" >> ~/.bashrc && source ~/.bashrc. Replace .bashrc with .zshrc if you’re using zsh instead of bash as your shell. [return]