(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
?
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.
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With