创建 Yeoman 生成器时出现问题

Problems creating Yeoman generator

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

尝试 1: 我安装了 generator-generator 模块没有错误,但是执行 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:很遗憾,这是一个 npm 错误。您应该将 npm 更新到最新版本,卸载并重新安装。这些错误发生在 npm 2 和 3 之间。

错误 2:确保您使用的是最新的哟。然后你可以 运行 DEBUG=* yo boilerplate 这会让你更深入地了解 Yeoman 在哪里搜索注册生成器以及为什么它找不到你链接的本地生成器。