Question

I've created a Yeoman custom generator. Within the index.js file I want to perform some text replacement on some files. In package.json I have added the dependency replace then when I require('replace') in index.js and run the generator, I get the error Cannot find module 'replace'. I have tried different modules from NPM and running the generator fails for all of them - it fails to find the module.

The appropriate part of package.json

  "dependencies": {
    "replace": "~0.2.9",
    "yeoman-generator": "~0.16.0",
    "chalk": "~0.4.0"
  },

Start of index.js

'use strict';
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var replace = require('replace');

var MyGenerator = yeoman.generators.Base.extend({
    init: function () {
        this.pkg = require('../package.json');

The generator fails when it hits the Replace require. Chalk and Yeoman Generator don't fail and they're loaded in the same way.

Why don't my added modules load?

Était-ce utile?

La solution

Did you run npm install after manually adding that line to package.json? The preferred way to install a package is by running: npm install --save _package_. It will download the latest release, and save it to your package.json.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top