Polymer: gulp error at build time: merge-stream

2019-09-16 07:53发布

问题:

I'm trying to run gulp to build my app like Rob Dodson explains here, but I get the following error:

module.js:327
    throw err;
    ^ Error: Cannot find module 'merge-stream'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/Users/path/to/gulpfile.js:16:21)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)

It appears there is some required module named merge-stream? Is this correct? If so, where and how can I get this module? Or is there some other problem causing this?

I just copied the files package.json, polymer.json and gulpfile.js from the sample code Rob supplied here.

gulpfile.js
'use strict';

// Documentation on what goes into PolymerProject.
const path = require('path');
const gulp = require('gulp');
const mergeStream = require('merge-stream');
const del = require('del');
const polymerJsonPath = path.join(process.cwd(), 'polymer.json');
const polymerJSON = require(polymerJsonPath);
const polymer = require('polymer-build');
const polymerProject = new polymer.PolymerProject(polymerJSON);
const buildDirectory = 'build/bundled';

/**
 * Waits for the given ReadableStream
 */
function waitFor(stream) {
  return new Promise((resolve, reject) => {
    stream.on('end', resolve);
    stream.on('error', reject);
  });
}

function build() {
  return new Promise((resolve, reject) => {
    // Okay, so first thing we do is clear the build
    console.log(`Deleting build/ directory...`);
    del([buildDirectory])
      .then(_ => {
        // Okay, now lets get your source files
        let sourcesStream = polymerProject.sources()
          // Oh, well do you want to minify stuff? Go for it! 
          // Here's how splitHtml & gulpif work
          .pipe(polymerProject.splitHtml())
            .pipe(gulpif(/\.js$/, uglify()))
            .pipe(gulpif(/\.css$/, cssSlam()))
            .pipe(gulpif(/\.html$/, htmlMinifier()))
          .pipe(polymerProject.rejoinHtml());

        // Okay now lets do the same to your dependencies
        let depsStream = polymerProject.dependencies()
          .pipe(polymerProject.splitHtml())
            .pipe(gulpif(/\.js$/, uglify()))
            .pipe(gulpif(/\.css$/, cssSlam()))
            .pipe(gulpif(/\.html$/, htmlMinifier()))
          .pipe(polymerProject.rejoinHtml());

        // Okay, now lets merge them into a single build stream.
        let buildStream = mergeStream(sourcesStream, depsStream)
          .once('data', () => {
            console.log('Analyzing build dependencies...');
          });

        // If you want bundling, do some bundling! Explain why?
        buildStream = buildStream.pipe(polymerProject.bundler);

        // If you want to add prefetch links, do it! Explain why?
        // buildStream = buildStream.pipe(new PrefetchTransform(polymerProject));

        // Okay, time to pipe to the build directory
        buildStream = buildStream.pipe(gulp.dest(buildDirectory));

        // waitFor the buildStream to complete
        return waitFor(buildStream);
      })
      .then(_ => {
        // You did it!
        console.log('Build complete!');
        resolve();
      });
  });
}

gulp.task('default', build);

回答1:

merge-stream is already declared as a devDependency in package.json, so the error indicates either you haven't installed the dependencies, or that package is somehow deleted from your dependency store (i.e., node_modules/).

To install only merge-stream, you'd run this from the project's root directory:

npm install merge-stream

or:

yarn add merge-stream

But it's typically required to install all dependencies for the build to succeed. Run this instead:

npm install

or:

yarn


标签: gulp polymer