". js" file generated by Gulp returns the error "require is not defined"

Asked

Viewed 237 times

1

I installed Gulp to concatenate and minify my files .js, but the following error occurs in the browser when I test the application:

Uncaught ReferenceError: require is not defined

I have the gulpfile.js as follows:

var gulp = require('gulp');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var uglifycss = require('gulp-uglifycss');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('js', function () {
    return gulp.src([
        'node_modules/jquery/dist/jquery.min.js',
        'node_modules/jquery-ui/jquery-ui.js',
        'node_modules/bootstrap/dist/js/bootstrap.min.js',
        'node_modules/amcharts3/amcharts/amcharts.js',
        'node_modules/amcharts3/amcharts/serial.js',
        'node_modules/amcharts3/amcharts/pie.js',
        'src/Havas/AdminBundle/Resources/public/js/chart.js',
        'src/Havas/AdminBundle/Resources/public/js/map.js',
        'src/Havas/AdminBundle/Resources/public/js/jquery-jvectormap-1.2.2/jquery-jvectormap-1.2.2.min.js',
        'src/Havas/AdminBundle/Resources/public/js/jquery-jvectormap-1.2.2/jquery-jvectormap-us-aea-en.js',
        'src/Havas/AdminBundle/Resources/public/js/main.js'
    ])
    .pipe(concat('javascript.js'))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('web/js'));
});

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

How to correct this error? What I did wrong?

  • 1

    "the following error occurs in the browser", the require is environment Node.js.

  • @Sergio yes this error appears in the browser console and is in the javascript concatenated and minified by Gulp. I understand that require is Nodejs environment, but this is Gulp output, I don’t understand what I did wrong.

  • @Sergio did not find the solution, however I started using Bower in the project with Gulp, it worked and no longer returns any errors in the browser.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.