gulp-imagemin jpegtran-bin does not work when I run the simple task

3.8k views Asked by At

I want to compress the images and pipe to the directory of build, but the build folder is empty after running the task. This is my task:

// Gulp.js configuration
// modules
const gulp = require('gulp')
const newer = require('gulp-newer')
const imagemin = require('gulp-imagemin')

// development mode?
// let devBuild = (process.env.NODE_ENV !== 'production')

// folders
const folder = {
  src: 'src/',
  build: 'build/'
}

gulp.task('images', function () {
  var out = folder.build + 'images/'
  return gulp.src(folder.src + 'images/**/*')
              .pipe(newer(out))
              .pipe(imagemin({optimizationLevel: 5}))
              .pipe(gulp.dest(out))
})

I got the error when I tried to running gulp images and can not found /node_modules/jpegtran-bin/vendor/jpegtran in the node_modules

[10:07:18] Using gulpfile ~/Desktop/MyProjects/project1/gulpfile.js
[10:07:18] Starting 'images'...

events.js:182
      throw er; // Unhandled 'error' event
      ^
Error: spawn /home/toantd/Desktop/MyProjects/project1/node_modules/jpegtran-bin/vendor/jpegtran ENOENT
at exports._errnoException (util.js:1026:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
at onErrorNT (internal/child_process.js:366:16)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)

I also tried to fix this issue follow some links:

Reinstall node modules

Npm rebuild

There is my project structure:

.
├── build
│   ├── css
│   ├── html
│   ├── images
│   └── js
├── gulpfile.js
├── package.json
├── src
│   ├── html
│   ├── images
│   │   ├── bg.jpg
│   │   └── my-logo.png
│   ├── js
│   └── scss
└── yarn.lock

Noted that I'm working under company proxy, but I'm sure that I already set proxy for both of npm and git.

Can anyone help me? Thank you in advance.

2

There are 2 answers

0
Doug E Fresh On

I had a similar issue. My vendor folder existed but did not have the exe in it. It might not be your exact problem. My solution was to run a rebuild on this package.

npm rebuild jpegtran-bin

I would like to know what caused this problem in the first place though. Seems to be a lack of support on the package itself.

0
krolovolk On

Try to install jpegtran-bin from npm

npm install --save jpegtran-bin