npm / npx

npm package executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong binary selected due due to simple typo in libnpx

maa105 opened this issue · comments

in libnpx (10.2.0 and 10.2.2)
when you run for example npx templator-generator some_folder it selects the generate-project binary of the templator-generator package instead of the templator-generator binary. I tracked it and found out the problem is in the library libnpx (I tried with node 12.6.1 and 12.8.4 default installations which have libnpx 10.2.0 and 10.2.2 respectively and both have the same bug)

the bug is in the file index.js in the libnpx library lines (92-94 in both 10.2.0 and 10.2.2) the lines are as follows:

  const cmd = new RegExp(`^${argv.command}(?:\\.cmd)?$`, 'i')
  const matching = bins.find(b => b.match(cmd))
  return path.resolve(results.bin, bins[matching] || bins[0])  // bug here

The Solution:
The bug indicated in the comment in above code is because matching is a string not an integer should be return path.resolve(results.bin, matching || bins[0]) (notice matching directly not bin[matching]).

So what was happening it was taking the bins[0] by default. which seems are sorted alphabetically. so as a workaround for my package mentioned above I created a new binary which is "alphabetically smallest" (so npx templator-generator some_folder now works). But I believe this is a must and an easy fix.