npx命令

作者: hanxianshe_9530 | 来源:发表于2019-10-18 12:44 被阅读0次

    https://github.com/zkat/npx
    https://github.com/junosuarez/awesome-npx
    npx是npm5.2新增的命令,npx 会帮你执行依赖包里的二进制文件。

    版本检查

    检查npx是否存在,不存在通过npm -i -g npx安装。
    npx -v

    不需要在 scripts 中声明

    在以往中,我们在 node 项目中要执行一个脚本,需要将它在 scripts 中声明

    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "init:runtime-only": "vue init webpack vue-cms"
    }
    

    然后执行命令,其实本质还是运行 vue init webpack vue-cms

    npm run init:runtime-only
    

    用了 npx 以后呢,你不需要在 scripts 中声明了

    npx vue init webpack vue-cms
    
    调用项目安装的模块(一般是node_modules里面的)
    # 常规操作
    node_modules/.bin/mocha --version
    # npx操作
    npx mocha --version
    

    原理:运行时会到项目模块路径和$PATH(环境变量)里检查命令是否存在

    避免全局安装模块
    # npx会把create-react-app下载到临时目录,使用完成后删除。
    npx create-react-app test-app
    
    # 没安装http-server时,用npm会报错,用npx就会自动下载同名的包并使用该命令
    npx http-server
    
    npx 甚至支持运行远程仓库的可执行文件
    npx github:piuccio/cowsay hello
    
    一条命令开启一个静态服务器
    npx http-server
    

    相关文章

      网友评论

          本文标题:npx命令

          本文链接:https://www.haomeiwen.com/subject/xheimctx.html