Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems creating Yeoman generator

I've tried two different approaches to creating a Yeoman generator, and both are failing. Here's where I'm currently at, but first with a couple of notes:

  • yo doctor passes all tests
  • I have fixed npm's permissions by changing the default install path, as detailed here.

Attempt 1: I installed the generator-generator module with no errors, but executing yo generator results in the following error:

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)

I took a guess and ran npm install -g download, but that didn't fix anything.

Attempt 2 I followed the steps outlined on Yeoman's Authoring page, but even after npm link resulted in a valid path to my new generator, yo boilerplate just resulted in a generator not installed error. The generator file structure is as follows:

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

The contents of package.json are:

{
  "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"
  }
}

The contents of app/index.js are (just to get something to work):

'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;

Any help would be greatly appreciated. Thanks!

like image 833
ridgehkr Avatar asked Oct 18 '22 18:10

ridgehkr


1 Answers

Error 1: That's unfortunately an npm error. You should update npm to the latest version, uninstall and reinstall. These errors happened a bunch between npm 2 and 3.

Error 2: Make sure you're on the latest yo. Then you can run DEBUG=* yo boilerplate and that'll give you more insight as to where Yeoman is searching to register generators and maybe why it is not finding your linked local generator.

like image 158
Simon Boudrias Avatar answered Oct 21 '22 14:10

Simon Boudrias