LinuxCommandLibrary

npx

Execute Node.js packages without global installation

TLDR

Execute the command from a local or remote npm package

$ npx [command] [argument1 argument2 ...]
copy

In case multiple commands with the same name exist, it is possible to explicitly specify the package
$ npx --package [package] [command]
copy

Run a command if it exists in the current path or in node_modules/.bin
$ npx --no-install [command] [argument1 argument2 ...]
copy

Execute a specific command suppressing any output from npx itself
$ npx --quiet [command] [argument1 argument2 ...]
copy

Display help
$ npx --help
copy

SYNOPSIS

npx [options] <command> [arguments...]
npx [options] -p|--package <package> <command> [arguments...]
npx [options] -- <command> [arguments...] (to run a command with the same name as an npx option)

PARAMETERS

--package <package> / -p <package>
    Specifies a package to install before executing the command. This is useful when the command name differs from the package name, or when you want to execute a specific version.

--yes / -y
    Automatically confirm all prompts, assuming "yes" for any questions.

--no-install
    Prevents npx from installing the package if it's not found locally. If the package isn't present, the command will fail.

--ignore-existing
    Forces npx to fetch and use the latest version of the package from the registry, even if a local version is already installed.

--npm <npm-client>
    Specifies which npm client (e.g., npm, yarn, pnpm) to use for installing packages.

--node-arg <arg>
    Passes an argument directly to the Node.js executable that npx uses to run the script.

--call <command-string> / -c <command-string>
    Executes the provided string as a shell command. This is useful for complex commands or piped commands.

--shell <path>
    Specifies the shell to use for executing the command. By default, npx uses your default shell.

--version
    Displays the npx version number.

--help
    Shows help information for npx.

DESCRIPTION

npx (Node Package eXecutor) is a command-line tool that comes bundled with npm (since version 5.2.0). It allows you to execute Node.js package binaries, typically command-line interface (CLI) tools, directly from the npm registry without needing to install them globally on your system or even locally in your project's node_modules directory. This is particularly useful for one-off commands, scaffolding new projects (like npx create-react-app), or trying out new tools without cluttering your global package space. npx intelligently checks if the package exists locally or in your cache; if not, it temporarily downloads it, executes the command, and then removes the temporary files. This ensures you're always using the latest version of a tool and helps maintain a clean development environment by avoiding global package pollution and version conflicts.

CAVEATS

npx requires an active internet connection to download packages if they are not already cached or locally installed.
While npx handles temporary installations, large packages can still take time to download, impacting execution speed for first-time runs.
Be cautious when executing arbitrary commands from the internet using npx, as they run with your user's permissions and could potentially be malicious. Always ensure you trust the source package.

TEMPORARY EXECUTION

npx prioritizes running locally installed executables. If not found, it temporarily downloads the package to a cache, runs the command, and then cleans up the temporary files, ensuring a clean system.

SCAFFOLDING TOOLS

npx is commonly used for scaffolding new projects with tools like create-react-app, vue-cli, or next which are designed for one-time use during project initialization.

HISTORY

npx was introduced with npm version 5.2.0 in July 2017. Before its introduction, developers often had to either globally install CLI tools (leading to version conflicts and system clutter) or add them as local devDependencies and then use npm run scripts to execute them. npx aimed to solve these problems by providing a straightforward way to execute Node.js binaries directly, enhancing developer workflow and improving dependency management practices by promoting local, temporary execution over global installations.

SEE ALSO

npm(1), yarn(1), pnpm(1)

Copied to clipboard