Search code examples
node.jsnpmnpx

running npx with scoped packages


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?


Solution

  • 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"
    }