'How to solve the Gulp error: imagemin.jpegtran is not a function
I am getting this error whenever I try to make imagemin work for any kind of image:
21:54:49] Starting 'imgmin'...
[21:54:49] 'imgmin' errored after 47 ms
[21:54:49] TypeError: imagemin.jpegtran is not a function
at imgmin (D:\Dropbox\Side Projects\JS learning\dev-gulp\gulpfile.js:92:26)
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 am currently using:
- node version 12.13.1
- npm version 6.12.1
- gullp cli version: 2.2.0
- gulp local version: 4.0.2
Here 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 found someone had the same error with gifsicle and fixed it by reinstalling the plugins but it didn't work for me.
Solution 1:[1]
I found the answer and I write it here in case someone will have the same issue. imagemin.jpegtran has been substitute by imagemin.mozjpeg. Also, I had to install mozjpeg (npm install --save-dev imagemin-mozjpeg) as, for the time I am writing, it seems it is not installed together with imagemin.
Solution 2:[2]
I had the same problem but it happened in the alpine version of node (node:16.11.1-alpine3.14) in docker.
The problem was that some OS libs were missing and it was solved by adding the libs (bash, autoconf, libtool, libltdl, libpng-dev, lcms2-dev, GCC, g++, automaker):
RUN apk add --update \
bash \
autoconf \
libtool \
libltdl \
libpng-dev \
lcms2-dev \
gcc \
g++ \
automake \
make \
nasm \
&& rm -rf /var/cache/apk/*
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andrea D_ |
Solution 2 | Thiago Valentim |