创建Yeoman生成器的问题

时间:2016-01-12 21:31:00

标签: node.js yeoman yeoman-generator

我尝试了两种不同的方法来创建Yeoman生成器,两者都失败了。这是我目前所处的位置,但首先是几个注意事项:

  • yo doctor通过了所有测试
  • 我修改了npm的权限,方法是更改​​默认安装路径as detailed here

尝试1: 我安装了生成器 - 生成器模块没有错误,但执行yo generator会导致以下错误:

module.js:328
throw err;
^

Error: Cannot find module 'download'
    at Function.Module._resolveFilename (module.js:326:15)
    at Function.Module._load (module.js:277:25)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/generator-generator/node_modules/yeoman-generator/lib/actions/fetch.js:3:16)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)

我猜了一下然后跑了npm install -g download,但这并没有解决任何问题。

尝试2 我按照Yeoman's Authoring page上列出的步骤进行了操作,但即使在npm link导致我的新生成器的有效路径后,yo boilerplate只是导致生成器未安装错误。生成器文件结构如下:

generator-boilerplate
   app
      index.js
   .yo-rc.json
   package.json

package.json的内容是:

{
  "name": "generator-boilerplate",
  "version": "0.1.0",
  "description": "A Yeoman generator for a standard front-end project",
  "files": [
    "app"
  ],
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "yeoman-generator",
    "boilerplate"
  ],
  "author": "<my name>",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "yeoman-generator": "^0.22.3"
  }
}

app / index.js的内容是(只是为了让某些东西起作用):

'use strict';

var util = require('util');
var path = require('path');
var generators = require('yeoman-generator');
var chalk = require('chalk');

var Boilerplate = generators.Base.extend({
    // The name `constructor` is important here
    constructor: function () {
        // Calling the super constructor is important so our generator is correctly set up
        generators.Base.apply(this, arguments);

        // Next, add your custom code
        this.option('coffee'); // This method adds support for a `--coffee` flag
    },

    promptUser: function() {
        var done = this.async();

        // have Yeoman greet the user
        console.log(this.yeoman);

        var prompts = [{
            name: 'appName',
            message: 'What is your app\'s name ?'
        },{
            type: 'confirm',
            name: 'addDemoSection',
            message: 'Would you like to generate a demo section ?',
            default: true
        }];

        this.prompt(prompts, function (props) {
            this.appName = props.appName;
            this.addDemoSection = props.addDemoSection;

            done();
        }.bind(this));
    }
});

module.exports = Boilerplate;

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:3)

错误1:遗憾的是npm错误。您应该将npm更新到最新版本,卸载并重新安装。这些错误发生在npm 2和3之间。

错误2:确保你是最新的哟。然后你可以运行DEBUG=* yo boilerplate,这将让你更深入地了解Yeoman搜索寄存器生成器的位置,以及为什么找不到链接的本地生成器。