sindresorhus / gulp-imagemin

Minify PNG, JPEG, GIF and SVG images
MIT License
1.9k stars 157 forks source link

TypeError: imagemin.jpegtran is not a function #340

Closed Andrea-Dispe closed 3 years ago

Andrea-Dispe commented 4 years ago

I am getting this error whenever I try to make imagemin work for any kind of image:

[17:03:45] Starting 'imgmin'... [17:03:45] 'imgmin' errored after 3.11 ms [17:03:45] TypeError: imagemin.jpegtran is not a function at imgmin (D:\Dropbox\Side Projects\JS learning\dev-gulp\gulpfile.js:85:18) at bound (domain.js:419:14) at runBound (domain.js:432:12) at asyncRunner (D:\Dropbox\Side Projects\JS learning\dev-gulp\node_modules\async-done\index.js:55:18) at processTicksAndRejections (internal/process/task_queues.js:75:11)

I have: node version 12.13.1 npm version 6.12.1 gullp cli version: 2.2.0 gulp local version: 4.0.2

this is my gulpfile.js:

`
let mainDir = 'gulp project test';

let gulp = require('gulp'),
    browserSync = require('browser-sync').create(),
    reload = browserSync.reload,
    autoprefixer = require('gulp-autoprefixer'),
    sass = require('gulp-sass'),
    cleansCSS = require('gulp-clean-css'),
    sourcemaps = require('gulp-sourcemaps'),
    concat = require('gulp-concat'),
    imagemin = require('gulp-imagemin'),
    changed = require('gulp-changed'),
    uglyfi = require('gulp-uglify'),
    lineec = require('gulp-line-ending-corrector');

//set directories variables
let root = `../${mainDir}/`,
    scss = root + 'sass',
    js = root + 'src/js',
    cssDist = root + 'dist/css'
jsDist = root + 'dist/js',
    imgSRC = root + 'src/images/*',
    imgDest = root + 'dist/images/*';

//set watch files
let phpWatcher = root + '**/*php', //glob pattern
    styleWatcher = scss + '**/*.scss';

//set the order I want Gulp to concat JS files
let jsSRC = [
    js + 'file1.js',
    js + 'file2.js',
    js + 'file3.js',
    js + 'file4.js',
    js + 'file5.js',
    js + 'file6.js',
];

//set the order I want Gulp to concat CSS files
let cssSRC = [
    root + 'src/css/file1.css',
    root + 'src/css/file2.css',
    root + 'src/css/file3.css',
    root + 'src/css/file4.css',
    cssDist + 'style.css'
];

function css() {
    return gulp.src(scss + 'style.scss')
        .pipe(sourcemaps.init({
            loadMaps: true
        }))
        .pipe(sass({
            outputStyle: 'expanded'
        }).on('error', sass.logError))
        .pipe(autoprefixer('last 2 versions'))
        .pipe(sourcemaps.write())
        .pipe(lineec())
        .pipe(gulp.dest(cssDist))
}

function concatCSS() {
    return gulp.src(cssSRC)
        .pipe(sourcemaps.init({
            loadMaps: true,
            largefile: true
        }))
        .pipe(concat('style.min.css'))
        .pipe(cleansCSS())
        .pipe(sourcemaps.write('./maps/'))
        .pipe(lineec())
        .pipe(gulp.dest(cssDist));
}

function javascript() {
    return gulp.src(jsSRC)
        .pipe(concat('project.js'))
        .pipe(uglyfi())
        .pipe(lineec)
        .pipe(gulp.dest(jsDist));
}

function imgmin() {
    return gulp.src(imgSRC)
        .pipe(changed(imgDest))
        .pipe(imagemin([
            imagemin.gifsicle({
                interlaced: true
            }),
            imagemin.jpegtran({
                progressive: true
            }),
            imagemin.optipng({
                optimizationLevel: 5
            })
        ]))
        .pipe(gulp.dest(imgDest));
}

function watch() {
    browserSync.init({
        open: 'external',
        proxy: 'http://localhost/dev',
        port: 8080,
    });
    gulp.watch(styleWatcher, gulp.series([css, concatCSS]));
    gulp.watch(jsSRC, javascript);
    gulp.watch(imgSRC, imgmin);
    gulp.watch([phpWatcher, jsDist + 'project.js', cssDist + 'style.min.css'])
}

exports.css = css;
exports.concatCSS = concatCSS;
exports.javascript = javascript;
exports.imgmin = imgmin;
exports.watch = watch;

let build = gulp.parallel(watch);
gulp.task('default', build);`

I seem unable to find any solution to this problem.

schoenkaft commented 4 years ago

Had the same problem. The issue is you're still calling imagemin.jpegtran in your config, which was replaced by imagemin.mozjpeg in v7.0.0.

// old
imagemin.jpegtran({
    progressive: true,
}),

// new
imagemin.mozjpeg({
    progressive: true,
}),

See also: https://github.com/sindresorhus/gulp-imagemin/commit/279a91b61c208c0e2a18d3734e6073f5e1d283a2#diff-0730bb7c2e8f9ea2438b52e419dd86c9R36

Andrea-Dispe commented 4 years ago

Had the same problem. The issue is you're still calling imagemin.jpegtran in your config, which was replaced by imagemin.mozjpeg in v7.0.0.

// old
imagemin.jpegtran({
    progressive: true,
}),

// new
imagemin.mozjpeg({
    progressive: true,
}),

See also: 279a91b#diff-0730bb7c2e8f9ea2438b52e419dd86c9R36

Thank you very much. It works now!