前端脚手架采坑记录

47 阅读1分钟

前端脚手架踩坑记录

脚手架代码实现

// cli.js
#!usr/bin/env node

console.log('hello world')

声明脚手架的方式

package.json文件bin字段

  1. 直接声明文件路径
{
    "name": "hallo-world"
    "bin": "./cli.js"
}
  1. 别名声明
{
    "name": "hello-world",
    "bin": {
        "hello": "./cli.js"
    }
}

测试脚手架

  1. 作为全局声明
# 将脚手架软链接到全局环境变量
$ npm link
# 执行
$ hello-world
  1. 项目本地声明
# 脚手架项目
$ yarn link
# 测试项目
$ yarn link "hello-world"

测试的项目package.json

{
    "script": {
        "hello-world": "hello-world"
    }
}

执行

$ yarn hello-world

作为本地npm script脚本运行

问题描述: 系统找不到指定的路径

{
    "script": {
        "hello-world": "hello-world"
    }
}
$ yarn hello-world
yarn run v1.22.11
$ /hello-world
系统找不到指定的路径。

直接原因:node_modules\.bin\hello-world.cmd 中的执行路径错误 根本原因:脚手架js文件头,指定的执行环境错误

#!/bin/env node
修改为
#!/usr/bin/env node

需要指定usr路径,即可解决问题,再次打包发布后运行,脚手架正常运行