Termux-packages: NPM ERR! Cannot read property 'length' of undefined

Created on 29 Nov 2017  路  20Comments  路  Source: termux/termux-packages

https://github.com/npm/npm/issues/19265

Is this a Termux-specific issue maybe?

Most helpful comment

For anyone else that runs into this, I just fixed it pretty simply with:

$ apt-get install yarn
$ yarn global add npm

Now npm will work.

All 20 comments

It happens with both nodejs and nodeks-current on Pixel 2

The comment at https://github.com/npm/npm/issues/19265#issuecomment-347997389 says that this has been fixed in the worker-farm dependency that npm uses, so it will be available after an npm update. So I'm closing this in favour of tracking the problem at https://github.com/npm/npm/issues/19265.

Thanks @fornwall. How do I perform an "npm update". For example, if I run npm i -g npm it will fail because of this error, so I can not update npm. Do we need to update the nodejs termux package?

@trusktr Right, we will have to wait for an updated nodejs version to bring in the npm fix.

I guess the best workaround is to apply https://github.com/rvagg/node-worker-farm/commit/0b2349c6c7ed5c51e234e418fad226875313e773 locally by hand, if possible.

You can alternatively set it to the number of virtual CPUs you have, (or the number of concurrent workers you want, if that's different.) This fix will effectively disable concurrent workers when receiving this error. If you like, you can try [WIP] https://github.com/Quasic/cpuinfo node package to find the number. I hope to turn this into a fix that will still use concurrent workers.

@trusktr How did you solve this issue? I still get this same error on pixel2 for node 9.6.1 and npm 5.6.0

Hi @MrClan, @fornwall linked to the commit above. You have to open that file (look at the error output to see where the file is) and modify the file just like in the commit.

@trusktr Perfect, thanks. Just as @fornwall suggestd, hard coded the value 8, like this: maxConcurrentWorkers: 8 in _/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js_ and issue solved.

For anyone else that runs into this, I just fixed it pretty simply with:

$ apt-get install yarn
$ yarn global add npm

Now npm will work.

Regarding the last comment, the correct command to install globally is:

yarn global add npm

But npm was then complaining it was installed by something else, tried to solve it this way:

yarn global remove npm
apt-get remove nodejs
apt-get install nodejs

But back to square one, waiting for this to be fixed upstream.

Thanks @LEI I updated my comment with your typo fix.

It's unfortunate that it doesn't do the trick for everyone.

Just do this

pkg install yarn
yarn global add npm
npm i -g npm

In termux node doesn't return the value properly for the os.cpus() that's why the error happens on the file
/data/data/com.termux/files/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js
Just edit the above mentioned file with maxConcurrentWorkers value set as 1

None of this works for me

If you grep the error log (/data/data/com.termux/files/home/.npm/_logs/*debug.log) for a line with farm.js you can find the right file to edit.

I have tried all the suggestions here and still cannot get npm install to work in Termux.
On a onePlus 6 phone
npm v. 6.5.0
node v. 11.6.0

tried installing yarn then adding npm via yarn. Tried editing the line in farm.js with no positive result.

Is there a fix for this?

I can see the changes || { length: 1 } already present in farm.js but it still throws the error

@hussainb It has these lines by default:
``` .js
const DEFAULT_OPTIONS = {
workerOptions : {}
, maxCallsPerWorker : Infinity
, maxConcurrentWorkers : (require('os').cpus() || { length: 1 }).length
, maxConcurrentCallsPerWorker : 10
, maxConcurrentCalls : Infinity
, maxCallTime : Infinity // exceed this and the whole worker is terminated

But `cpus()` will throw TypeError on latest Android versions. You need to replace entire `(require('os').cpus() || { length: 1 }).length` with 1.

***
Patch:
``` .diff
--- /data/data/com.termux/files/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js.orig
+++ /data/data/com.termux/files/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js
@@ -3,7 +3,7 @@
 const DEFAULT_OPTIONS = {
           workerOptions               : {}
         , maxCallsPerWorker           : Infinity
-        , maxConcurrentWorkers        : (require('os').cpus() || { length: 1 }).length
+        , maxConcurrentWorkers        : 1
         , maxConcurrentCallsPerWorker : 10
         , maxConcurrentCalls          : Infinity
, maxCallTime : Infinity // exceed this and the whole worker is terminated

@hussainb It has these lines by default:

const DEFAULT_OPTIONS = {
           workerOptions               : {}
         , maxCallsPerWorker           : Infinity
        , maxConcurrentWorkers        : (require('os').cpus() || { length: 1 }).length
         , maxConcurrentCallsPerWorker : 10
         , maxConcurrentCalls          : Infinity
, maxCallTime : Infinity // exceed this and the whole worker is terminated

But cpus() will throw TypeError on latest Android versions. You need to replace entire (require('os').cpus() || { length: 1 }).length with 1.

Patch:

--- /data/data/com.termux/files/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js.orig
+++ /data/data/com.termux/files/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js
@@ -3,7 +3,7 @@
 const DEFAULT_OPTIONS = {
           workerOptions               : {}
         , maxCallsPerWorker           : Infinity
-        , maxConcurrentWorkers        : (require('os').cpus() || { length: 1 }).length
+        , maxConcurrentWorkers        : 1
         , maxConcurrentCallsPerWorker : 10
         , maxConcurrentCalls          : Infinity
, maxCallTime : Infinity // exceed this and the whole worker is terminated

Thank you @xeffyr for replying.
npm install works after changing maxConcurrentWorkers to 1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bkdwt picture bkdwt  路  3Comments

roycebank picture roycebank  路  3Comments

divyakutty picture divyakutty  路  3Comments

roalyr picture roalyr  路  3Comments

tigran123 picture tigran123  路  3Comments