本文共 3401 字,大约阅读时间需要 11 分钟。
npx 运行react
In this post, I want to introduce a very powerful command that’s been available in starting version 5.2, released in July 2017: npx.
在这篇文章中,我想介绍一个非常强大的命令,一直在提供启动5.2版本,在2017年7月发布:NPX。
If you don’t want to install npm, you can
如果您不想安装npm,则可以
npx
lets you run code built with Node and published through the npm registry.
npx
允许您运行使用Node构建并通过npm注册表发布的代码。
Node developers used to publish most of the executable commands as global packages, in order for them to be in the path and executable immediately.
节点开发人员过去通常将大多数可执行命令发布为全局包,以使它们立即位于路径中并可执行。
This was a pain because you could not really install different versions of the same command.
这很痛苦,因为您无法真正安装同一命令的不同版本。
Running npx commandname
automatically finds the correct reference of the command inside the node_modules
folder of a project, without needing to know the exact path, and without requiring the package to be installed globally and in the user’s path.
运行npx commandname
自动在项目的node_modules
文件夹中找到该命令的正确引用,而无需知道确切的路径,也不需要在全局和用户路径中安装该软件包。
There is another great feature of npm
, which is allowing to run commands without first installing them.
npm
另一个重要功能是无需先安装命令即可运行命令。
This is pretty useful, mostly because:
这非常有用,主要是因为:
1) you don’t need to install anything 2) you can run different versions of the same command, using the syntax @version
1)您不需要安装任何内容2)您可以使用@version语法运行同一命令的不同版本
A typical demonstration of using npx
is through the cowsay
command. cowsay
will print a cow saying what you wrote in the command. For example:
使用npx
典型演示是通过cowsay
命令。 cowsay
将打印出一头母牛,说出您在命令中写的内容。 例如:
cowsay "Hello"
will print
cowsay "Hello"
将打印
_______< Hello > ------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Now, this if you have the cowsay
command globally installed from npm previously, otherwise you’ll get an error when you try to run the command.
现在,如果您之前已从npm全局安装了cowsay
命令,那么这是要执行的操作,否则,将在运行命令时收到错误消息。
npx
allows you to run that npm command without having it installed locally:
npx
允许您运行该npm命令,而无需将其本地安装:
npx cowsay "Hello"
will do the job.
会做的工作。
Now, this is a funny useless command. Other scenarios include:
现在,这是一个有趣的无用命令。 其他方案包括:
running the vue
CLI tool to create new applications and run them: npx vue create my-vue-app
运行vue
CLI工具以创建新应用程序并运行它们: npx vue create my-vue-app
creating a new React app using create-react-app
: npx create-react-app my-react-app
使用create-react-app
创建一个新的React应用: npx create-react-app my-react-app
and many more.
还有很多。
Once downloaded, the downloaded code will be wiped.
一旦下载,下载的代码将被擦除。
Use the @
to specify the version, and combine that with the :
使用@
指定版本,并将其与结合使用:
npx node@6 -v #v6.14.3npx node@8 -v #v8.11.3
This helps to avoid tools like nvm
or the other Node version management tools.
这有助于避免使用nvm
等工具或其他Node版本管理工具。
npx
does not limit you to the packages published on the npm registry.
npx
并不限制您使用npm注册表中发布的软件包。
You can run code that sits in a gist, for example:
您可以运行位于要点中的代码,例如:
npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32
Of course, you need to be careful when running code that you do not control, as with great power comes great responsibility.
当然,在运行不受控制的代码时,您需要格外小心,因为强大的功能带来了巨大的责任。
翻译自:
npx 运行react
转载地址:http://dvqgb.baihongyu.com/