Does npx no longer do install-less run?
Asked Answered
M

2

35

From the nodejs.dev site:

npx allows you to run that npm command without installing it first. If the command isn't found, npx will install it into a central cache:

They use this as an example:

npx cowsay "Hello"

But when I run that:

$ npx cowsay "Hello"

Need to install the following packages:
  cowsay
Ok to proceed? (y)

Huh? Is there some preference that I need to set now? I'm used to npx running things without installing them, like they say on nodejs.dev. I don't really want to install cowsay in my globals.

Node v14.17.5
NPM 7.21.0
OS:ProductName: Mac OS X
ProductVersion: 10.15.7

Filed as an issue here: https://github.com/nodejs/nodejs.org/issues/4104

Edit: just tested this in NPM 6 and it works as expected. This might be a change in NPM post v6.

✗ npm --version
6.14.16

✗ npx cowsay "Hello"

npx: installed 41 in 7.509s
 _______
< Hello >
...
Mann answered 31/8, 2021 at 23:1 Comment(3)
ran across the exact same issueMetage
From the answers below, it looks like in NPM versions 6 and below NPX was installed the package without prompting and then executed the command. Apparently now it just adds a prompt to install the package before doing so.Galileo
There's an answer that says exactly that https://mcmap.net/q/432566/-does-npx-no-longer-do-install-less-runMann
M
4

This appears to be a change after NPM 6.

$ nvm install 18
Downloading and installing node v18.0.0...
...

$ nvm use 18  # this was a fresh install 
Now using node v18.0.0 (npm v8.6.0)


$ npx cowsay "Node 18"
Need to install the following packages:
  cowsay
Ok to proceed? (y) y
 _________
< Node 18 >
 ---------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ nvm use 14
Now using node v14.19.1 (npm v6.14.16)

$ npm uninstall --global cowsay # in case I already had it installed
up to date in 0.037s

$ npx cowsay "Node 14"  # NOTE: no confirmation 
npx: installed 41 in 5.271s
 _________
< Node 14 >
 ---------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Note: no confirmation for the install in Node 14.

If you want to use a package without confirming the install in Node 7+ you'll need to add the confirm flag. See @Nitesh's answer.

Mann answered 21/4, 2022 at 21:37 Comment(0)
N
29

You can do following if you are not willing to type 'yes' everytime you install.

npm_config_yes=true npx cowsay "hello"

See this https://github.com/npm/cli/issues/2226

npx also has a --yes flag you can use to bypass the prompt:

npx --yes some-npm-package

This is undocumented if you run npx --help, but the documentation for this flag is hidden in the command's "description" on the NPM website.

There is also a --no flag available if you need to reject the prompt instead.

Nanosecond answered 31/8, 2021 at 23:28 Comment(4)
There is also a --yes flag you can use instead. The "moderators" apparently don't like that being posted as an answer, so here's a link instead: https://mcmap.net/q/278263/-automatically-accept-installing-npx-package-duplicateCincinnatus
@Cincinnatus I guess the mod decided it was too similar. I added your text to the answer.Mann
-y is the same as --yesIllustrator
Note that the --yes / -y flag is only available in NPM 7+. It is not available in 6.x, since NPM 6 does not prompt for confirmation, as per https://mcmap.net/q/432566/-does-npx-no-longer-do-install-less-run. docs.npmjs.com/cli/v9/commands/…Univalence
M
4

This appears to be a change after NPM 6.

$ nvm install 18
Downloading and installing node v18.0.0...
...

$ nvm use 18  # this was a fresh install 
Now using node v18.0.0 (npm v8.6.0)


$ npx cowsay "Node 18"
Need to install the following packages:
  cowsay
Ok to proceed? (y) y
 _________
< Node 18 >
 ---------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

$ nvm use 14
Now using node v14.19.1 (npm v6.14.16)

$ npm uninstall --global cowsay # in case I already had it installed
up to date in 0.037s

$ npx cowsay "Node 14"  # NOTE: no confirmation 
npx: installed 41 in 5.271s
 _________
< Node 14 >
 ---------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Note: no confirmation for the install in Node 14.

If you want to use a package without confirming the install in Node 7+ you'll need to add the confirm flag. See @Nitesh's answer.

Mann answered 21/4, 2022 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.