Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用 npx 更高效地执行 npm 命令 #2

Open
shhider opened this issue Nov 15, 2017 · 0 comments
Open

用 npx 更高效地执行 npm 命令 #2

shhider opened this issue Nov 15, 2017 · 0 comments

Comments

@shhider
Copy link
Owner

shhider commented Nov 15, 2017

今天在看 react 文档时突然发现多了这么一句:

If you have npm 5.2.0+ installed, you may use npx instead.

npx create-react-app my-app

cd my-app
npm start

npx 是什么?查了下,[email protected] 都已经内置了。几天没看新闻,这是又跟不上脚步了啊。。。

npx 其实已经面世有段时间了,截至今天,版本号已经到 9.7.1了。项目介绍是execute npm package binaries,用于执行 npm 包的二进制文件。

我看到这也是懵逼的,看几个例子先。

举例

npx [options] <command>[@version] [command-arg]...

最基本的就是执行一个命令,npx 从几个地方找相关的执行文件:1、$PATH;2、当前项目(也有称之为本地)的node_modules/.bin目录。如果都没有找到,就下载安装相关的 npm 包并执行。

比如本文开头的npx create-react-app my-app。首先在本地找是否有create-react-app可执行文件,如果找到就执行create-react-app my-app;如果没找到,就先安装create-react-app包,然后接着执行create-react-app my-app

要注意一下的是,新安装的包是属于临时安装,不会安装到 global 或 local。

如果不想自动下载缺失的包呢,添加参数--no-install

npx github:piuccio/cowsay

https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32

npx 还可以指定远程的文件执行。

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant