如何查找原因 - 错误:找不到模块

时间:2015-02-08 16:18:07

标签: javascript gulp browserify commonjs

我在浏览器中使用gulp来捆绑应用。虽然构建功能正常,但我不断收到以下错误消息:

Error: Cannot find module 'function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}'

有没有人有任何想法可能导致错误或如何调试它?唯一不出现错误的时间是主js文件中有require个语句。以下是gulp和main.js文件的参考:

gulpfile

var gulp, browserify, stringify, sass, concat, uglify, source, buffer;

gulp = require('gulp');
browserify = require('gulp-browserify');
stringify = require('stringify');
sass = require('gulp-sass');
concat = require('gulp-concat');
uglify = require('gulp-uglify');

function handleError(err) {
    console.log(err.toString());
}

gulp.task('browserify', function () {
    return gulp.src('./src/js/**/*.js', { read: false })
               .pipe(browserify({
                   transform: [stringify({
                       extensions: ['.html'],
                       minify: true
                   })]
               }))
               .on('error', handleError)
               .pipe(concat('main.js'))
               .pipe(gulp.dest('./bin/js'));
});

gulp.task('sass', function() {
    return gulp.src('./src/scss/**/*.scss')
               .pipe(sass())
               .on('error', handleError)
               .pipe(concat('main.css'))
               .pipe(gulp.dest('./bin/css'));
});

gulp.task('index', function () {
  return gulp.src('./src/index.html')
             .pipe(gulp.dest('./bin'));
});

gulp.task('default', ['browserify', 'sass', 'index']);

gulp.task('watch', function() {
  gulp.watch('./src/js/**/*.js', ['browserify']);
  gulp.watch('./src/scss/**/*.scss', ['sass']);
  gulp.watch('./src/index.html', ['index']);
  gulp.watch('./src/html/**/*.html', ['browserify']);
});

main.js

'use strict';

var $ = require('jquery'),
    ko = require('knockout'),
    greet = require('./components/greet');

window.$ = window.jQuery = $;

function AppViewModel() {
    var self = this;

    self.userName = ko.observable('');
}

ko.components.register('greet', greet);
ko.applyBindings(new AppViewModel());

1 个答案:

答案 0 :(得分:0)

当您连接多个浏览器化文件时会发生这种情况。要解决这个问题,你只需要在browserify之前进行连接

gulp.task('browserify', function () {
    return gulp.src('./src/js/**/*.js', { read: false })
               .pipe(concat('main.js'))
               .pipe(browserify({
                   transform: [stringify({
                       extensions: ['.html'],
                       minify: true
                   })]
               }))
               .on('error', handleError)
               .pipe(gulp.dest('./bin/js'));
});