npx

从 npm 包运行命令

概要

npx -- <pkg>[@<version>] [args...]
npx --package=<pkg>[@<version>] -- <cmd> [args...]
npx -c '<cmd> [args...]'
npx --package=foo -c '<cmd> [args...]'

描述

此命令允许您在与通过 npm run 运行类似的上下文中从 npm 包(本地安装或远程获取)运行任意命令。

--package 选项指定的任何包都将在执行命令的 PATH 中提供,以及任何本地安装的包可执行文件。--package 选项可以指定多次,以在所有指定包都可用的环境中执行提供的命令。

如果本地项目依赖项中不存在任何请求的包,则将它们安装到 npm 缓存中的文件夹中,该文件夹在执行过程中添加到 PATH 环境变量中。打印一个提示(可以通过提供 --yes--no 来抑制)。

不带说明符的包名称将与本地项目中存在的任何版本匹配。仅当具有与本地依赖项完全相同的名称和版本时,带有说明符的包名称才会被视为匹配。

xY5Gimv+Mrtzz9u5v8g7kLh2utXiuoMs5edik8aGOUuafuKCoD8bGumjjmv9dXv8tTJf1fndTmqWmHZ/V4BKp1NX2xivEmZzlnCd1TdVHqgWzc/2mKqPod/3F0Wi6Ex8InnhFJpRg3oxtIoL0eOmvBn7It+/etjDRQct6x+uYlxlu1kA3z3Ccfl366813mpLc7cQcaSJSSv5FKKpVXZXpQHX1HV/4z6sZJRN0R1ztw5JS8HmOWB2H9kvjdDghXrLrSZy99DETstJNR3BR0SU9wgeDS7yf71sguzhUN8cok1wTobun4og99QZWyDVNQQJxBm1XalXk5pGYtkS5BJBQDtNjuKXdp1wB+pZOVPrmmTzvMxZLt9SoriTUv9OVi49

    rfyLJ+lL8QKa2weq3XkUfhhBOBhaZ+l6PA3tAlQylL92VHGUuY+HV4ss56XoNvgOMv9INu3ZQ4o8VBERKqHQrsWQBiAdoTO1v/e804EYIWir37HJAvsYQKyBtWdMRpdzaqP80Vs743Zu/eU0auarhwmuf299owHlolPCc3UwhaN2kyVAlJ7BSH4iUaP6d4ZTEoKQJxoBbOZ4HvpP9YSjFjCbOKJNOOLXYyivIXNYenau5Bdti8TEPgcjGZ5pawISGP0OubZotKjbhAByhioqvRQra8mh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XghOgwztkdGpgm/d3qn78dii/We26JtEII6w9vHl7lHsdzIzt8MtFRTW/NJ7wpdM4/C0kDiVCnFRvQeb5YhXI7sKWh+vrTU6U4yPdBmmiIPmqaKvP3qmbMZM/+6sO5Iw

$ foo bar --package=@npmcli/foo

yJIZaScU2eA3Eumq2ymb37QV2Vus4YEF1hNOSstbmWMWJc2di5mrxln53c9DvxegEVAkoKxLKiRl9sanQ15zZFRkvqgS7jKNvau8alPydBFDV7uvao4beBXCMUYahyNP3JaA2ydbw3EynMY0oxVevQ==

26r4e7gP+dRk4QLPlNoJhyM4Sbnu1voMF6YnupIsYIAQ2hGdsQ/Dq6hHAlCaD03p1C6SlCQAUBYdtjKTz5fv4mOyFk+Mm6Ffn5PhYjGpZQYryGAqbwPo0hz76NTV3eYO

$ npm exec foo@latest bar --package=@npmcli/foo

XghOgwztkdGpgm/d3qn78UAZLVoX9ORKvW/IF3p5BAyA1rhVcvbF0IaWKEBQh+lJ3lLMl895qKQUuErmP0h4OcMy5FPQ8owoYxsb6Q8RsJ0Za53VXfkIsUIITW8T58gFKIhu15zEnY+M0T/efj6/+rJ3E5oCC1r5RCluM4r3qO8dqih87uhA2qXqSJU9haNgW/QHAr8Y5sY3MjFcIGF4fMV0vP5gtD626yHBWFtTn/Q=

$ foo@latest bar

UJax4uRMVfuRMe3cDA8YmdYXmF/fcYpeYti+agM/xhSyqaBdfVBsyzs+2jluyJrreA7MzNk1kdqA3mpOUEoK2eLxcqGrKqw0Zvhy6dnuj8bBI5fdUf5Mf0Cu1SLR2BroLmUAiFd+vIVnHyaHxGQ2fXj8VWp0b0dPNA3BoN+jvOot6MLucvtQQatqMDvfyfpSF157ud+PXwhiy3Ia4AmmpQ==

$ npm exec -- foo@latest bar --package=@npmcli/foo

示例

FoezYGJ9meK0OaNa5myvCJKTv4ZgW8eSB5AJjhKJsFIx73d1PSYBIMbshG2AlXY2mfS8DMgo004JyBQWXKaw8UJiC6NzIsw2uakOOtGYl74=

$ npm exec -- tap --bail test/foo.js
$ npx tap --bail test/foo.js

vkEsU2NgEGBPeOx5nGY9U/+P9HLlWHCVspE1pkn1VTcot+zCjKpagnehKZor4qEGTvsOxKK3JB/wADfIp7sE7ZBs8z5ogd1fDYYUajnFjzPKhhE9NlIxmt4yycHJVQx9PkXGd7KmqdC36EC08eWifg==

$ npm exec --package=foo -- bar --bar-argument
# ~ or ~
$ npx --package=foo bar --bar-argument

gk2f/2r8wsKoisFvmJAhhvlubcpzn/qpniQPKwhj8DQqFjQPnx06+nhh2V93eFeY9weIwpMLlgFmNQQ1ZsRssg==

$ npm x -c 'eslint && say "hooray, lint passed"'
$ npx -c 'eslint && say "hooray, lint passed"'

与旧 npx 版本的兼容性

g4BI6MII7pQtTdzebG0MA9wCd+FLhQtmxzOxRbAfVqHdZsgTzy/yasbdASHm4tjGWsgxNDg1OzB80MUCPPaZ/m1NBKTYMawTYR+YcgZL+/XNOCv1D/ORXUVavqA6F+OFmurdkZBqo8IHcbwpaWVLaLV4B54YPJdupdtOth8jwPKU71c0txxN6AkOzOX/g1mo8xTa7RFwvlVH+BbN1/yoNz1aZN+mcuWI/nCL0JIb3beeOtXGdZBrgC7QOb2nx3yCoAHQMeEudO6vz4oYJbDML8IDDJGKXHG3tOwEG9nELx9ZuYTIM320kTUjGJzY+OpXDAxLcQB2av0d7BqIXsoGJZzMbRIM8paw46RxlpvEzWDXtUcxn12sTyCkcuRWjebNdKVa+yqQgsQ4Yz9YcMw6kA==

wbrz+NmIS7vAWzT+N6iYV8lMQXzvghu9N7rJ4BzcRGha7sqmHWuJn3/Kp5keOUUG

    lZlc9lZB7qbgIDsVRoD6Z1tvb9w+biZ0KWk/yy0Ph9r9SqwzV8/LrHH3qvOIMrbp+Pthz6iYzuuj14fRQk0xI0xJsxMID+pOM6Q2UYldr8tOJDjaCSlUia7lGe2s/fchiMrI3SJVNYYZaAn9SLvwRk74pOsvGm/EChHg5M+72PNtAFW6m0NgnffCwjmAtM/jPUFzzcNar2Vdnw4TQMXTAC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