Search code examples
node.jsgulpgulp-concat

require is not defined error on gulp-concat


I want to uglify and combine my js files with gulp. Here is my code

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pump = require('pump');
var gutil = require('gulp-util');
gutil.env.type = 'production';
gulp.task('uglify', function (cb) {
return gulp.src([
    'pure/modernizr.js',
    'pure/horizon.js'
])
        .pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest("ugly"));
});
var sourcemaps = require("gulp-sourcemaps");
var concat = require("gulp-concat-js");
gulp.task("concat", function () {
return gulp.src([
    'ugly/modernizr.js',
    'ugly/horizon.js'
])
        .pipe(sourcemaps.init())
        .pipe(concat({
            "target": "concatenated.js", // Name to concatenate to 
            "entry": "./main.js" // Entrypoint for the application, main module 
                    // The `./` part is important! The path is relative to 
                    // whatever gulp decides is the base-path, in this 
                    // example that is `./lib` 
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest("pure/"));
});

I end up with some code enclosing my js files from obfuscator stating:

//// THIS FILE IS CONCATENATED WITH gulp-obfuscator-js

When I include this in code, it throws require is not defined, I surf around the web and found one similar question. But that answer is also not clear for me. I believe that I miss some small thing here, since I am new to gulp.


Solution

  • The issue here is I have used gulp-concat-js which obfuscate your js. I should have used gulp-concat. May help someone.