Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

npx getting started tutorial

npx getting started tutorial

If you are just starting to learn JavaScript or Node.js, you have likely already come across npm to install various tools and libraries. Sometimes we just want to quickly run a tool to see how it works without installing it globally?

Another situation is that different projects need different versions of command-line tools, and global installation always leads to conflicts?

npx is a powerful tool created to solve these problems.

npx is a command-line tool introduced in npm version 5.2.0 that lets you execute npm packages without installing them globally.

Simply put,npx is a convenient tool for executing Node.js packages, and its core design philosophy is:Use packages temporarily without permanently installing them。

A vivid analogy

Imagine the toolbox in your home:

Use npx, you can run any package from the npm registry without first installing it on your computer.

Traditional pain points How npx solves them
Must install globally Run directly
Version conflicts Specify a version each time
Environment pollution Can be cleaned up after use
Local commands are hard to find Automatically locate

Why use npx?

1. Solving global package pollution and version conflicts

in npx Before it appeared, if you wanted to use a scaffolding tool like create-react-app、vue-cli on the command line, you had to install it globally first:npm install -g create-react-app。

This brings two problems:

npx lets you specify a version to run directly and leave after use, perfectly avoiding these problems.

2. Easily run packages installed locally in a project

In a project directory, packages you install via npm install some-cli-tool (which appear in the node_modules/.bin/ directory) can usually only be invoked through complex paths or npm scripts. But npx can intelligently prioritize the local version in the current project, making invocation very simple.

3. Safely and conveniently try new tools

Want to test a newly released command-line tool? Just use npx new-cool-tool to try it out! If it doesn't suit you, it won't leave any trace on your computer.


How npx works

When you execute npx , it follows this logic to find and execute commands:

This process is completely transparent to the user; you only need to enjoy the convenience it brings.


How to use npx?

Environment Preparation

Make sure your Node.js version is >= 5.2.0; you can check with the following command:



node --version

npm --version

# If npm version >= 5.2.0, npx is already available

npx --version

Basic Syntax



npx [选项] <命令>[@版本] [命令的参数...]

Description of common options and parameters

Option Full name Description
--no-install Force npx to only use packages that already exist locally; if not present locally, report an error without downloading.
--ignore-existing Ignore packages that already exist locally and force downloading the latest version from remote.
-p, --package <包名> Specify the packages to install. This is very useful when you need to use multiple packages at the same time.
-c <命令字符串> When using -p When multiple packages are specified, use -c to tell npx the specific command to execute.
--yes Automatically answer "yes" in cases known to trigger a prompt (such as create-react-app asking whether to continue).

Practical Application Examples

Let's look at a few concrete examples to see how npx really shines.

Example 1: Running a one-time command (the most common scenario)

You want to use cowsay , this fun little tool that makes a cow speak in the terminal, but you don't want to install it.

Step 1: Run directly with npx



npx cowsay hello

Code explanation:npx checks whether cowsayis available locally; if not, it automatically downloads from npm and cleans up after the command finishes.

Expected output:

npx cowsay hello



Need to install the following packages:

cowsay@1.6.0

Ok to proceed? (y) y

 _______

< hello >

 -------

        \   ^__^

         \  (oo)\_______

            (__)\       )\/\

                ||----w |

                ||     ||



Step 2: Run with a specified version If you want to use a specific version:



npx cowsay@1.4.0 hello1.4.0

Output:

 ____________

< hello1.4.0 >

 ------------

        \   ^__^

         \  (oo)\_______

            (__)\       )\/\

                ||----w |

                ||     ||

Example 2: Creating a project with a scaffolding tool

Take creating a React project as an example:

Traditional approach (no longer recommended):



npm install -g create-react-app  # 全局安装

create-react-app my-app          # 使用

Problems with this installation approach:

Modern approach (using npx):



npx create-react-app my-app

Code explanation:npx temporarily fetches the latest version of create-react-app, uses it to scaffold the project, and does not leave create-react-app in your global environment afterward. Next time you create a project, it will still fetch the latest version.

Example 3: Running project-local tools

Suppose you installed webpack as a development dependency (npm install webpack --save-dev)。

Traditional invocation (very troublesome):



# Needs a long path input

./node_modules/.bin/webpack --version



# Or configure scripts in package.json

# "scripts": { "build": "webpack" }

# Then run

npm run build

Using npx (very concise):



npx webpack --version

# or run build directly

npx webpack

Code explanation:npx intelligently prioritizes finding the project's node_modules under webpack to execute.

Example 4: Executing scripts from GitHub

npx You can even run code from a GitHub gist or repository.



npx https://gist.github.com/username/abcdefg

Please note that running unknown code from the internet carries security risks; always ensure the source is trustworthy.


npx's three core capabilities

1. Directly run any npm package

General format:



npx 包名

Example:



npx cowsay hello

directly runs a command-line little cow 🐮

2. Run project-local dependencies

Many tools are installed only in the project:



npm install vite

Previously you had to:



./node_modules/.bin/vite

Now:



npx vite

Automatically finds the local executable for you

3. Execute with the latest version (without polluting the system)



npx create-next-app@latest my-app

Always use the latest scaffolding tool

No need to upgrade global tools


Most common npx scenarios

Create project



npx create-react-app my-app

npx create-next-app

npx vue-cli create my-vue

npx degit user/repo

Temporary tools



npx eslint .

npx prettier --write .

npx serve .

Run the Development Server



npx vite

npx nodemon app.js


Advanced tips and precautions

1. Combining multiple packages

Sometimes a command depends on multiple packages. For example, you want to start a simple HTTP server in a directory and use browser-sync for live reloading at the same time. Although uncommon, npx can do it:



npx -p http-server -p browser-sync -c 'http-server & browser-sync start --proxy "localhost:8080" --files "**/*"'

Code explanation:

2. Precautions