【问题标题】:Problems creating Yeoman generator创建 Yeoman 生成器的问题
【发布时间】:2016-01-12 21:30:59
【问题描述】:

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

  • yo doctor 通过所有测试
  • 我已通过更改默认安装路径 as detailed here 修复了 npm 的权限。

尝试 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;

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: node.js yeoman yeoman-generator


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多