Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run multiple binaries from a single module via npx?

Tags:

node.js

npm

npx

(Note: I'm using the cowsay module here to illustrate my point)

Without installing a module on my computer I can run it directly via npx with:

npx cowsay Hello!

If I have multiple binaries specified in package.json.

"bin": {
    "cowsay": "./cli.js",
    "cowthink": "./cli.js"
},

I'd also like to do

npx cowthink Hello!

But this fails. Is this because npx runs the module named cowsay and just runs the first command it finds in the "bin" field in package.json?

Is there a way to run cowthink directly from npx?

like image 246
dgwyer Avatar asked Dec 01 '18 14:12

dgwyer


People also ask

Is NPX better than npm?

Which is better npm vs npx? If the package in issue is only to be used once or twice, rather than every time the project runs, it is preferable to utilize NPX, which will execute the package without installing it. NPM is used to install packages, which we should do if our project requires dependencies or packages.

How does NPX command work?

Using NPX. With NPX, you can run and execute packages without having to install them locally or globally. When running NPM executables with NPX, if a package is installed, NPX will search for the package binaries (either locally or globally) and then run the package.

What is the difference between npm vs NPX?

Npm is a tool that use to install packages. Npx is a tool that use to execute packages. Packages used by npm are installed globally. You have to care about pollution in the long term.

Does NPX install dependencies?

npx : An npm package runner — helps to execute packages without installing explicitly. npx makes it easy to install and manage dependencies hosted in npm registry. It simplifies the process and provides a better for executables.


1 Answers

As the documentation states,

Unless a --package option is specified, npx will try to guess the name of the binary to invoke depending on the specifier provided

If binary and package names don't match, this is what --package option is for:

npx -p cowsay cowthink Hello!
like image 78
Estus Flask Avatar answered Oct 06 '22 23:10

Estus Flask