Browserify无法找到孩子所需的依赖项

时间:2017-04-20 22:13:23

标签: npm gulp browserify

我有一个带有简单index.js的存储库:

(function() {
   "use strict";

   var angular = require('angular');
})();

我使用gulp捆绑(编辑中下面的完整文件):

gulp.task('browserify', function() {
    return browserify({
        entries: './dist/childRepository.js',
        insertGlobals : true
    })
    .transform(to5ify)
    .bundle()
    .on('error', errorWarning)
    .pipe(source('childRepository.bundle.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('dist'))
});

这会以正确的顺序创建我的捆绑文件,并在浏览器中正常运行。现在在另一个存储库中,我使用npm创建了第一个依赖项。

npm install childRepository.git --save

在第二个存储库中,我创建了另一个index.js:

(function() {
    "use strict";

    var angular = require('angular');

    var childRepository = require('childrepository');
})();

我有一个类似的浏览器和捆绑的gulp文件,但它失败并出现错误:

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: Cannot find module './angular' from '/Users/jrquick/uabshp/childRepository/dist'
    at /Users/jrquick/node_modules/browser-resolve/node_modules/resolve/lib/async.js:55:21
    at load (/Users/jrquick/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
    at onex (/Users/jrquick/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
    at /Users/jrquick/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:123:15)

我尝试了几种设置,添加源映射,翻转标记但无法解决此错误。有人有建议吗?谢谢。

编辑,我的package.json用于childRepository:

    {
  "name": "childRepository",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/childRepository.bundle.js",
  "directories": {
    "example": "example"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@bitbucket.org/uabshp/childRepository.git"
  },
  "author": "",
  "license": "ISC",
  "homepage": "https://bitbucket.org/uabshp/childRepository#readme",
  "devDependencies": {
    "6to5ify": "^4.1.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-jshint": "^2.0.4",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^3.0.0",
    "gulp-sourcemaps": "^2.6.0",
    "gulp-uglify": "^2.0.0",
    "jshint": "^2.9.4",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  },
  "dependencies": {
    "angular": "^1.6.0"
  }
}

package.json for paren repository:

    {
  "name": "parentrepository",
  "version": "1.0.0",
  "description": "### How do I get set up? ###",
  "main": "./dist/parentRepository.bundle.js",
  "directories": {
    "example": "example"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@bitbucket.org/uabshp/parentRepository.git"
  },
  "author": "",
  "license": "ISC",
  "homepage": "https://bitbucket.org/uabshp/parentRepository#readme",
  "devDependencies": {
    "6to5ify": "^4.1.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-jshint": "^2.0.4",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^3.0.0",
    "gulp-sourcemaps": "^2.6.0",
    "gulp-uglify": "^2.0.0",
    "jshint": "^2.9.4",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  },
  "dependencies": {
    "angular": "^1.6.4",
    "childRepository": "git+ssh://git@bitbucket.org/uabshp/childRepository.git"
  }
}

gulpfile.js(两个名称除外):

var gulp = require('gulp');

var concat = require('gulp-concat');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var jshint = require('gulp-jshint');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var to5ify = require('6to5ify');
var uglify = require('gulp-uglify');

gulp.task('sass', function() {
    return gulp.src('sass/*.sass')
        .pipe(sass())
        .pipe(gulp.dest('dist/css'));
});

gulp.task('lint', function() {
    return gulp.src('src/**/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

gulp.task('scripts', function() {
    return gulp.src('src/*.js')
        .pipe(concat('childRepository.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('childRepository.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});

gulp.task('browserify', function() {
    return browserify({
            entries:       './dist/childRepository.js',
            insertGlobals: true,
            standAlone:    true
        })
        .transform(to5ify)
        .bundle()
        .on('error', errorWarning)
        .pipe(source('childRepository.bundle.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('dist'))
});

gulp.task('watch', function() {
    gulp.watch('src/*.js', ['build']);
    gulp.watch('dist/childRepository.js', ['browserify']);
    gulp.watch('sass/*.sass', ['sass']);
});

gulp.task('build', [
    'sass',
    'lint',
    'scripts',
    'browserify'
]);

gulp.task('default', [
    'build',
    'watch'
]);

function errorWarning(error) {
    console.log(error.toString());
    this.emit('end');
}

1 个答案:

答案 0 :(得分:0)

我通过安装derequire解决了这个问题:npm install gulp-derequire --save-dev

然后添加到我的gulpfile.js:

gulp.task('browserify', function() {
    return browserify('./dist/uabError.js')
       .transform(to5ify)
       .bundle()
       .on('error', errorWarning)
       .pipe(source('uabError.bundle.js'))
       .pipe(derequire())
       .pipe(buffer())
       .pipe(sourcemaps.init({loadMaps: true}))
       .pipe(sourcemaps.write('./'))
       .pipe(gulp.dest('dist'))
});

这确实会导致出现一个问题,即我收到有关尝试多次加载角度的警告。我还有另一项工作,不需要取消。只需将ignoreMissing设置为true即可。

gulp.task('browserify', function() {
    return browserify('./dist/uabError.js', { ignoreMissing: true })
       .transform(to5ify)
       .bundle()
       .on('error', errorWarning)
       .pipe(source('uabError.bundle.js'))
       .pipe(buffer())
       .pipe(sourcemaps.init({loadMaps: true}))
       .pipe(sourcemaps.write('./'))
       .pipe(gulp.dest('dist'))
});