Search code examples
node.jsshellprettiernpx

npx - what does the `@` do in `npx prettier` vs `npx prettier@2`?


I'm familiar with node_modules/.bin and the npx tool.

I've recently noticed one of our configs runs:

npx prettier@2

Which actually produces different output from

npx prettier

It seems like prettier is using a different config file when called this way.

What does the npx prettier@2 do differently from npx prettier?

Edit:

As requested:

$ npx prettier --version
2.1.1

$ npx prettier@2 --version
npx: installed 1 in 1.437s
2.2.0

Prettier 2.2.0 was released a few hours ago and seems to have a bug


Solution

  • npx will cause a package to be downloaded and execute bin scripts provided by that package. The command npx prettier will cause the latest version of the prettier to be downloaded and the file ./bin/prettier.js will be executed.

    npx also allows you specify which specific semantic version you want to download with the @ notation. So npx prettier runs latest, but npx prettier@2 will still run only version 2 even when prettier updates to a new major version.

    See npx package docs:

    npx [options] <command>[@version] [command-arg]...
    

    and

    -p, --package <package> - define the package to be installed. This defaults to the value of <command>. This is only needed for packages with multiple binaries if you want to call one of the other executables, or where the binary name does not match the package name. If this option is provided <command> will be executed as-is, without interpreting @version if it's there. Multiple --package options may be provided, and all the packages specified will be installed.


    A somewhat deeper look at your question makes things a little bit weirder though. I'm unsure why you get different results when you run both commands, they should be equivalent (right now at least since 2 is the current major version).

    Try printing the version string with both and see if you get a difference, that might show some additional details

    npx prettier --version
    npx prettier@2 --version
    

    Both give me the same string, but that might be different depending on your cache or config.