2

npx & yarn create & npm init

 2 years ago
source link: https://segmentfault.com/a/1190000041352775
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

npx & yarn create & npm init

TL;DR:

👉 npx xxx

👉 yarn create

👉 npm init

以 create-react-app 为例, 文档提供三种创建应用程序的方案:

  • npx create-react-app my-app
  • npm init react-app my-app
  • yarn create react-app my-app

都好用,为啥呢?

NPM (Node Package Manager) and NPX (Node Package Executor)

NPX 是包的执行器。试一下~

// npx cowsay 亖混子
npx: 41 安装成功,用时 6.739 秒
 _______________
< 亖混子 >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

用途一、以简短的方式执行本地包的命令

若执行项目依赖包的执行文件,需要键入以下内容:$ ./node_modules/.bin/jest ; 若使用 npx 将会非常简洁 $ npx jest

用途二、 一次性执行全局模块命令

有些全局包如 create-react-app 使用频率低,在需要时用 npx 即可 。对比全局安装,可保证使用最新版本

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

yarn create

create-* 的包快速创建项目。

使用方式 yarn create <starter-kit-package> [<args>]

命令底层做了两件事情:

  1. 全局安装 starter-kit-package 包,若本地已存在,尝试更新到最新版本
  2. 运行包的可执行文件

yarn create react-app my-app 和下面的脚本等价

$ yarn global add create-react-app
$ create-react-app my-app

npm init

create-* 包快速创建项目。和 yarn create 的作用和操作完全一样

npm init react-app my-app 等同于 yarn create react-app my-app

npm inityarn create 均利用包名规则 create-*,先全局下载、再执行

npx xxx没有包名约束,临时下载、执行后删除

三者效果一样,均使用最新包执行命令


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK