When you type an illegal npm
command, you are getting a user-friendly error message stating which commands are legal to use:
$ npm illegal
Usage: npm <command>
where <command> is one of:
add-user, adduser, apihelp, author, bin, bugs, c, cache,
completion, config, ddp, dedupe, deprecate, docs, edit,
explore, faq, find, find-dupes, get, help, help-search,
home, i, info, init, install, isntall, issues, la, link,
list, ll, ln, login, ls, outdated, owner, pack, prefix,
prune, publish, r, rb, rebuild, remove, repo, restart, rm,
root, run-script, s, se, search, set, show, shrinkwrap,
star, stars, start, stop, submodule, tag, test, tst, un,
uninstall, unlink, unpublish, unstar, up, update, v,
version, view, whoami
As you may notice, among others there is the isntall
command.
What is the point of this command? If this was created to handle typos, then why doesn't it have a special handling for intall
, insatll
etc? Besides, uninstall
doesn't have a corresponding unisntall
option.
(Using npm 1.3.22
version).
The reason I ask is that I'm a bit surprised and confused about how the typo is handled. For example, git
compares the command you've entered and suggests the closest commands it has available:
$ git stats
git: 'stats' is not a git command. See 'git --help'.
Did you mean this?
status
Also, pip
Python package manager has a similar functionality built-in:
$ pip isntall
ERROR: unknown command "isntall" - maybe you meant "install"
FYI, under-the-hood it uses difflib.get_close_matches()
function.
I was the original reporter of the connected issue on GitHub, and those were in my first days of contributing to open source software. I thought it was just a typo, and maybe a pull request would solve it. But I was blessed with some good sense to first raise an issue (#2933) to find out if they were looking for contribution on that front.
As it turned out, it unraveled a whole discussion around the issue, which was good to see.
The reason why the isntall
command exists is because the npm
maintainers believe that it saves them time — and thus, by extension — several other developers. As has been discussed in the linked issue, this was a contentious decision, and several people have suggested more interesting methods for resolving typos such as using the Levenshtein distance calculation of the illegal command from valid npm commands (https://www.npmjs.org/package/levenshtein).
At any rate, I presume if you do implement one of those algorithms and contribute to the npm project, it would be a nice addition to this awesome library.