简体   繁体   English

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

[英]How to Find Cause of — Error: Cannot find module

I'm using gulp with browserify, to bundle an app. 我将gulp与browserify结合使用,以捆绑应用程序。 While the build is functional, I continually receive the following error message: 在构建正常运行时,我不断收到以下错误消息:

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

Does anyone have any ideas what could be causing the error or how to debug it? 有谁知道导致错误的原因或如何调试? The only time the error does NOT appear is when there are zero require statements in the main js file. 仅当主js文件中的require语句为零时,才不会出现该错误。 For reference below are the gulp and main.js files: 以下是gulp和main.js文件供参考:

gulpfile gulp文件

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 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());

This happens when you concat multiple browserified files. 当您合并多个浏览器化的文件时,会发生这种情况。 To solve that you have simply to concat before 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'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM