Node has its own NPM module, so you can use the NPX command directly. In case it doesn’t work, install it manually.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

$ npm install -g npx

Invoke the module installed by the project

The main problem NPX is trying to solve is to call modules installed inside the project. For example, Mocha, a testing tool, is installed in-house.

$ npm install -D mocha

In general, Mocha can only be called in the scripts fields of project scripts and package.json. If you want to call Mocha from the command line, you must do something like this.

$node-modules/.bin/mocha –version

NPX is intended to solve this problem by making it easier to use modules installed inside the project, simply by calling them as follows.

$ npx mocha –version

NPX checks for the presence of the command in node_modules/. Bin and the environment variable $PATH.

Since NPX checks the environment variable $PATH, system commands can also be invoked.

# = ls $NPX ls

Note that the commands built into Bash are not in $PATH, so they don’t work. For example, CD is the Bash command, so you can’t use NPX CD.

Avoid installing modules globally

In addition to calling project internal modules, NPX can also avoid globally installed modules. For example, the create-react-app module is installed globally, and NPX can run it without global installation.

$ npx create-react-app my-react-app

While the above code is running, NPX downloads create-react-app to a temporary directory to be used and deleted later. If you run the preceding command again, create-react-app will be downloaded again.

NPX allows you to specify the version when downloading a global module.

$NPX [email protected] main.js -o./dist/main.js

The above code specifies the use of version 3.1.0 of the Uglip-js compression script.

Note that modules with the same name will be downloaded as long as the modules following the NPX cannot be found locally. 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 parameter and –ignore-existing parameter

If you want NPX to force the use of local modules instead of downloading remote ones, use the –no-install parameter. If the module does not exist locally, an error is reported.

$ npx –no-install http-server

Conversely, if you ignore a local module of the same name and force the installation to use a remote module, you can use the –ignore-existing argument. For example, if create-react-app is installed globally locally, but you still want to use remote modules, use this parameter.

$ npx –ignore-existing create-react-app my-react-app

Use different versions of Node

Using NPX’s ability to download modules, you can specify a version of Node to run scripts. The trick is to use the NODE module of NPM.

$NPX [email protected] -v v0.12.8

The above command will execute the script using Node version 0.12.8. The principle is to download this version of Node from NPM, use it and then delete it.

In some cases, this method can be used to switch Node versions more conveniently than a version manager like NVM.

The -p parameter

The -p parameter is used to specify the module to be installed by NPX, so the command in the previous section could be written as follows.

$NPX -p [email protected] node -v v0.12.8

Install [email protected] and then run the node -v command.

The -p parameter is useful for scenarios where multiple modules need to be installed.

$ npx -p lolcatjs -p cowsay [command]

– c parameters

If NPX installs multiple modules, by default, only the first executable of the command executed will use the module installed by NPX, and the subsequent executables will be interpreted by the Shell.

$NPX – p lolcatjs -p cowsay ‘cowsay hello | lolcatjs’ # error

The above code, cowsay hello | lolcatjs executes error, the reason is that the first cowsay the NPX the explanation, and the second command localcatjs is interpreted by a Shell, but there is no global lolcatjs and installed, so an error.

The -c argument interprets all commands with NPX. With this, the following code should execute normally.

$ npx -p lolcatjs -p cowsay -c ‘cowsay hello | lolcatjs’

Another function of the -c argument is to insert an environment variable into the command to be executed. For example, NPM provides some environment variables for the current project, which can be viewed with the following command.

$ npm run env | grep npm_

The -c argument carries these NPM environment variables into the NPX command.

NPX −c ‘echo “NPX -c ‘echo” NPX − C ‘echo “npM_package_name”

The above code prints the project name of the current project.

Execute GitHub source code

NPX can also execute the GitHub module source code.

Code executed Gist NPX https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32 code # # warehouse NPX lot: piuccio/cowsay hello

Note that the remote code must be a module, that is, it must contain package.json and entry scripts.