6

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 Answer 1

26

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']
};
Sign up to request clarification or add additional context in comments.

4 Comments

Can't believe that was the problem. Thanks a lot.
@BenDavis Can't believe it either... I had the same problem today.
Just ran into this myself. What a drag since path.join strips the preceding "./"
I had the same problem on the CLI browserify ... --require src/js/main.js. Using browserify ... --require ./src/js/main.js solved the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.