Gulp with browserify: Cannot find module src/js/ma

2019-04-28 07:15发布

问题:

I'm trying to get a basic build set up using Gulp and browserify, but keep seeing this error when trying to run the default task:

Error: Cannot find module 'src/js/main.js' from '/Users/ben/dev/git/myapp/'

gulpfile.js

var gulp = require('gulp');
var browserify = require('browserify');
var del = require('del');
var source = require('vinyl-source-stream');


var paths = {
    main_js: ['src/js/main.js'],
    js: ['src/js/*.js']
};

gulp.task('clean', function(done) {
    del(['build'], done);
});

gulp.task('js', ['clean'], function() {
    browserify(paths.main_js)
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('./build/'));
});

gulp.task('watch', function() {
    gulp.watch(paths.js, ['js']);
});

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

main.js

console.log("Hello!")

myapp/

.
├── gulpfile.js
├── node_modules
│   ├── browserify
│   ├── del
│   ├── gulp
│   └── vinyl-source-stream
├── npm-debug.log
├── package.json
└── src
    ├── css
    ├── index.html
    └── js
        └── main.js

I can't understand why it's failing to find main.js. When I run this command from myapp/, it works fine:

$ browserify src/js/main.js > build/bundle.js

回答1:

Try using "./src/js/main.js" instead of "src/js/main.js" i.e:

var paths = {
    main_js: ['./src/js/main.js'],
    js: ['src/js/*.js']
};