使用作用域包运行 npx

Ell*_*son 6 node.js npm npx

我正在尝试使用 npx 执行作用域包。似乎唯一的方法是直接指定包,例如:

npx -p @foo/bar bar
Run Code Online (Sandbox Code Playgroud)

这将正确下载@foo/bar并运行bar我的package.json“bin”部分中的条目:

"bin": {
    "bar": "./cli.js"
}
Run Code Online (Sandbox Code Playgroud)

但是,我真正想要的是输入:

$ npx @foo/bar
npx: installed 1 in 4s
npx: command not found: bar
Run Code Online (Sandbox Code Playgroud)

我试过@foo/bar, foo/bar,bar在该bin部分中,没有运气。npx 是否支持这样的作用域包?

Ell*_*son 8

好吧,看起来只要您不导出任何备用命令,作用域包就可以工作。也就是说,您不能使用对象形式,而必须仅指定一个 bin 命令:

{
    "name": "@foo/bar",
    ...,
    "bin": "./cli.js"
}
Run Code Online (Sandbox Code Playgroud)