A guide to Node.js, Part 5: npm and npx
Today, in the fifth part of the translation of the Node.js manual, we will finish the analysis of the npm capabilities, in particular, we will touch upon such issues as finding the installed versions of npm-packages, installing old versions of packages, updating dependencies, local and global uninstalling packages. Here we will talk about npx.
[Советуем почитать]Other parts of the [/b] cycle.
General information and getting started
Part 2: jаvascript, V? some tricks of development
Part 3: Hosting, REPL, work with the console, modules
Part 4: npm, package.json and package-lock.json files
Part 5: npm and npx
npx , which appeared in npm 5.2. One of its features is the launch of executable files that are part of npm-packages. We have already considered using npx to run a similar file from the cowsay package. Now let's talk about this in more detail.
▍Use npx to simplify the launch of local commands
Node.js-developers published a lot of executable files (utilities) in the form of packages that were supposed to be installed globally, which provided convenient access to their capabilities, since they could be launched from the command line simply by typing the name of the corresponding command. However, working in such an environment was very uncomfortable if you had to install different versions of the same packages.
Application of the command type
npx commandnameleads to an automatic search for the desired file in the project folder
node_modules. This eliminates the need to know the exact path to a similar file. It also makes it unnecessary to install the package globally with access to it from anywhere in the file system by using the system variable
▍Using utilities without having to install them
In npx there is one more interesting possibility, thanks to which utilities can be launched without their preliminary installation. This is useful mainly for the following reasons:
- No installation of utilities is required.
- You can run different versions of the same utilities, indicating the correct version using the design
Let's look at how to use this mechanism, using the example of the utility
already known to you. cowsay. So, if the cowsay package is installed globally, execute the command
on the command line. cowsay "Hello"will lead to the conclusion in the console of the "talking" cow:
< Hello >
^ __ ^
|| ---- w |
If the cowsay package is not installed globally, a similar command will generate an error.
The npx utility allows you to run such commands without installing them. It looks like, within the framework of our example, so:
npx cowsay "Hello"
Such a command will work, but although the "talking" cow, by and large, does not bring much benefit, the same approach can be used to perform much more useful commands. Here are some examples:
- There is a command line tool for creating and running Vue applications. With the use of npx it can be called so:
npx vue create my-vue-app.
- To create React-applications, you can use the utility
create-react-app. Its call through npx looks like this:
npx create-react-app my-react-app.
After downloading and using the appropriate code, npx will remove it.
▍Chart jаvascript code using different versions of Node.js
In order to run some code using different versions of Node.js, you can, with the use of npx, access the npm-package
node, indicating its version. It looks like this:
npx node @ 6 -v # v???
npx node @ 8 -v # v???
This allows you to abandon the use of tools like nvm or other version managers Node.js.
▍ Running arbitrary code snippets available at a certain address
Npx allows you to run not only the code published in npm. In particular, if you have a link to a piece of code (say, published on GitHub gist), you can run it as
Of course, when you run this code, you should not forget about security. Npx gives the developer great opportunities, but they also mean a big responsibility.
Today we talked about some useful npm mechanisms and about using npx. At this stage, you should have a basic understanding of the npm device and the methods of working with this package manager. If you want to learn more deeply npm - refer to page documentation of the project and more experiment.
Next time, we will discuss some of the basic mechanisms of Node.js, understanding which is necessary for the successful development of applications for this platform.
Dear readers! Do you use npx?
It may be interesting
Can I find someone to write my paper for me free? At our cheap for-pay academic help service with writers across all subjects. Discover more about us here. write a paper online free