Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running npx with scoped packages

Tags:

node.js

npm

npx

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?

like image 499
Elliot Nelson Avatar asked Oct 31 '19 18:10

Elliot Nelson


People also ask

Will NPX use local package?

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.

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.

What is scoped package in npm?

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.


1 Answers

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"
}
like image 97
Elliot Nelson Avatar answered Oct 18 '22 09:10

Elliot Nelson