npm Starting from version 5.2, the npx command has been added. It has many uses. This article introduces the main usage scenarios of this command.
Node has its own npm module, so you can use the npx command directly. If it doesn’t work, install it manually.
$ npm install -g npx
Call the module installed by the project
The main problem that npx wants to solve is to call the modules installed inside the project. For example, the testing tool Mocha is installed inside the project .
$ npm install -D mocha
Generally speaking, Mocha can only be called in the project script and package.json
fields. If you want to call it on the command line, it must be like the following.
# 项目的根目录下执行 $ node-modules/.bin/mocha --version
npx just wants to solve this problem and make the modules installed inside the project more convenient to use, just call it like the following.
$ npx mocha --version
The principle of npx is very simple, that is, when running, it will go to the
path and environment variables
to check whether the command exists.
Since npx checks environment variables
, system commands can also be called.
# 等同于 ls $ npx ls
Note that Bash’s built-in commands are not in
it, so they cannot be used.
For example, it
is a Bash command, so it cannot be used
Avoid installing modules globally
In addition to calling the internal modules of the project, npx can also avoid modules installed globally.
this module is installed globally, npx can run it, and it is not installed globally.
$ npx create-react-app my-react-app
When the above code runs, npx will
download to a temporary directory and delete it after use.
Therefore, if you execute the above command again in the future, it will download again
When downloading global modules, npx allows you to specify the version.
$ npx firstname.lastname@example.org main.js -o ./dist/main.js
The above code specifies the use of the
Note that as long as the module behind npx cannot be found locally, the module with the same name will be downloaded.
For example, if the
not installed locally
, the following command will automatically download the module and start a web service in the current directory.
$ npx http-server
If you want npx to force the use of local modules instead of downloading remote modules, you can use
If the module does not exist locally, an error will be reported.
$ npx --no-install http-server
Conversely, if you ignore the local module with the same name and force the installation to use the remote module, you can use
For example, if the local has been installed globally
, but you still want to use the remote module, use this parameter.
$ npx --ignore-existing create-react-app my-react-app
Use different versions of node
With the feature that npx can download modules, you can specify a certain version of Node to run scripts. Its trick is to use npm’s node module .
$ npx email@example.com -v v0.12.8
The above command will use the 0.12.8 version of Node to execute the script. The principle is to download this version of node from npm and delete it after use.
In some scenarios, this method is used to switch the Node version, which is more convenient than a version manager like nvm.
The parameter is used to specify the module to be installed by npx, so the command in the previous section can be written as follows.
$ npx -p firstname.lastname@example.org node -v v0.12.8
The above command specifies the installation first
, and then executes the
Parameters are useful for scenarios where multiple modules need to be installed.
$ npx -p lolcatjs -p cowsay [command]
If npx installs multiple modules, by default, among the executed commands, only the first executable item will use the module installed by npx, and the subsequent executable items will still be given to Shell for explanation.
$ npx -p lolcatjs -p cowsay 'cowsay hello | lolcatjs' # 报错
In the above code,
cowsay hello | lolcatjs
an error will be reported when executed, because the first item
is explained by npx, and the second command
is explained by Shell, but
it is not installed globally, so an error is reported.
The parameters can interpret all commands with npx.
With it, the following code can be executed normally.
$ npx -p lolcatjs -p cowsay -c 'cowsay hello | lolcatjs'
Another function of parameters is to bring environment variables into the command to be executed.
For example, npm provides some environment variables of the current project, which can be viewed with the following command.
$ npm run env | grep npm_
Parameters can bring these npm environment variables into the npx command.
$ npx -c 'echo "$npm_package_name"'
The above code will output the project name of the current project.
Execute GitHub source code
npx can also execute the module source code on GitHub.
# 执行 Gist 代码 $ npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32 # 执行仓库代码 $ npx github:piuccio/cowsay hello
Note that the remote code must be a module, that is, it must include
and entry scripts.