0

I have this gulp code that works fine with browser-sync and the gulp sass compiler, I've tried to insert a browserify task but seems doesn't work, it works if I type on the command line:

browserify src/js/main.js -o src/js/bundle/bundle.js 

this is my project structure:

|-project |--/src |---/css |----style.css |---/js |----main.js |----/bundle |-----bundle.js |---/scss |----_bootstrap.scss |----style.scss |---/assets |----/img |---index.html |--gulpfile.js |--package.json 

and this is my gulp file:

const gulp = require('gulp'); const browserSync = require('browser-sync').create(); const sass = require('gulp-sass'); const browserify = require('browserify'); gulp.task('sass', () => { return gulp.src("./src/scss/*.scss") .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest("./src/css")) .pipe(browserSync.stream()); }); gulp.task('js',()=>{ return gulp.src('./src/js/main.js') .pipe(browserify()) .pipe(gulp.dest('./src/js/bundle')) }); gulp.task('serve', ()=> { browserSync.init({ injectChanges: true, server: "./src" }); gulp.watch("./src/scss/*.scss", gulp.series('sass')); gulp.watch("./src/js/*.js", gulp.series('js')); gulp.watch("./src/*.html").on('change', browserSync.reload); }); gulp.task('default', gulp.series('serve','sass','js')); 

I have recently added the 'js' task but when I type gulp in the command line everything works fine except for the browserify task. as a test the main.js file looks like this:

const jquery = require('../../node_modules/jquery') console.log(jquery) 

and I still get the error 'required is not defined'. there is something i missed out. Many thanks

7
  • Possible duplicate of Client on node: Uncaught ReferenceError: require is not defined Commented Jan 28, 2018 at 20:25
  • I don't think so, the OP of the post you linked didn't know that he has to use tools like browserify to make the things works, I did.. my problem is to make browserify works fine with gulp, so please.... Commented Jan 28, 2018 at 20:30
  • What scripts are you including in your index.html and in what order? Commented Jan 28, 2018 at 20:32
  • for now just one: <script src="js/bundle/bundle.js"></script> Commented Jan 28, 2018 at 20:32
  • does this help? (see second answer). Commented Jan 28, 2018 at 20:33

1 Answer 1

0

ok, finally works (seems...) it was not a problem with browserify but with the new version of gulp, instead of:

gulp.task('default', gulp.series('serve','sass','js')); 

I have had to do:

gulp.task('default', gulp.parallel('serve','sass','js')); 

before the sass and js task didn't start, now with parallel function seems works

Sign up to request clarification or add additional context in comments.

2 Comments

Can you run them in series but in a different order? Does that help?
hi, it works also with only gulp.series, I have to put the serve task to the end, like that: gulp.task('default', gulp.series('sass','js','HTMLminify','serve'));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.