I'm trying to execute a scoped package using npx. It seems like the only way to do it is specify the package directly, e.g.:
npx -p @foo/bar bar
Which will correctly download @foo/bar
and run the bar
entry in my package.json
"bin" section:
"bin": {
"bar": "./cli.js"
}
But, what I really want is to type this:
$ npx @foo/bar
npx: installed 1 in 4s
npx: command not found: bar
I've tried @foo/bar
, foo/bar
, bar
in the bin
section, no luck. Does npx support scoped packages like this?
npx is a replacement for installing global packages. It encourages you to install packages locally, but still be able run them as if they were global, just with npx . Make sure you --save or --save-dev the package first. This keeps dependent packages listed in package.
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.
Scopes are a way of grouping related packages together, and also affect a few things about the way npm treats the package. Each npm user/organization has their own scope, and only you can add packages in your scope. This means you don't have to worry about someone taking your package name ahead of you.
OK, it looks like scoped packages work as long as you don't export any alternate commands. That is, you cannot use the object form and must instead specify only one bin command:
{
"name": "@foo/bar",
...,
"bin": "./cli.js"
}
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