npx tutorial
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
scripts
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
node_modules/.bin
path and environment variables
$PATH
to check whether the command exists.
Since npx checks environment variables
$PATH
, system commands can also be called.
# 等同于 ls $ npx ls
Note that Bash’s built-in commands are not in
$PATH
it, so they cannot be used.
For example, it
cd
is a Bash command, so it cannot be used
npx cd
.
Avoid installing modules globally
In addition to calling the internal modules of the project, npx can also avoid modules installed globally.
For example,
create-react-app
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
create-react-app
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
create-react-app
.
When downloading global modules, npx allows you to specify the version.
$ npx uglify-js@3.1.0 main.js -o ./dist/main.js
The above code specifies the use of the
uglify-js
compression script
version 3.1.0
.
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
http-server
module is
not installed locally
, the following command will automatically download the module and start a web service in the current directory.
$ npx http-server
--no-install
Parameters and
--ignore-existing
parameters
If you want npx to force the use of local modules instead of downloading remote modules, you can use
--no-install
parameters.
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
--ignore-existing
parameters.
For example, if the local has been installed globally
create-react-app
, 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 node@0.12.8 -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.
-p
parameter
-p
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 node@0.12.8 node -v v0.12.8
The above command specifies the installation first
node@0.12.8
, and then executes the
node -v
command.
-p
Parameters are useful for scenarios where multiple modules need to be installed.
$ npx -p lolcatjs -p cowsay [command]
-c parameter
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
cowsay
is explained by npx, and the second command
localcatjs
is explained by Shell, but
lolcatjs
it is not installed globally, so an error is reported.
-c
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'
-c
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_
-c
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
package.json
and entry scripts.
Reference link
(over)