Polymer:构建时的gulp错误:merge-stream

时间:2017-01-21 04:33:17

标签: gulp polymer

I'm trying to run gulp to build my app like Rob Dodson explains here,但我收到以下错误:

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)

看来有一些名为merge-stream的必需模块?它是否正确?如果是这样,我在哪里以及如何获得此模块?或者是否还有其他问题导致这种情况发生?

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 个答案:

答案 0 :(得分:1)

merge-stream已经声明为devDependency in package.json,因此错误表明您尚未安装依赖项,或者以某种方式从依赖项存储中删除了该包(即{{1} }})。

要仅安装node_modules/,您可以从项目的根目录运行此代码:

merge-stream

或:

npm install merge-stream

但是,通常需要为构建安装所有依赖项才能成功。改为运行:

yarn add merge-stream

或:

npm install