问题
the env:
- windows 7
- node 6.9.5
I use the plugin like this npm. but I get an error. from some video, It's correct. It's relative to imagemin's version?
// gulpfile.js
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var app = {
srcPath: 'src/',
devPath: 'build/',
prdPath: 'dist/', //生产部署
}
gulp.task('image', function() {
gulp.src(app.srcPath + 'image/**/*')
.pipe(gulp.dest(app.devPath + 'image'))
.pipe(imagemin())
.pipe(gulp.dest(app.prdPath + 'image'))
})
when I "gulp image", got an error:
[13:05:56] Using gulpfile ~\Desktop\study\angularapp\gulpfile.js
[13:05:56] Starting 'image'...
[13:05:56] Finished 'image' after 6.96 ms
[13:05:56] gulp-imagemin: Couldn't load default plugin "gifsicle"
[13:05:56] gulp-imagemin: Couldn't load default plugin "jpegtran"
[13:05:56] gulp-imagemin: Couldn't load default plugin "optipng"
[13:05:56] gulp-imagemin: Minified 1 image (saved 0 B - 0%)
I use npm install imagemin-optipng
, But it still doesn't work.
dependencies like this:
"devDependencies": {
"clean-css": "^4.0.5",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.1",
"gulp-connect": "^5.0.0",
"gulp-cssmin": "^0.1.7",
"gulp-imagemin": "^3.1.1",
"gulp-less": "^3.3.0",
"gulp-load-plugins": "^1.5.0",
"gulp-uglify": "^2.0.1",
"lodash": "^4.17.4",
"open": "^0.0.5"
}
what happend to it?
回答1:
I have the same problems and I found it's because default plugins were not installed correctly.
npm install imagemin-jpegtran imagemin-svgo imagemin-gifsicle imagemin-optipng --save
This warning was throw by these lines code #L12
const loadPlugin = (plugin, args) => {
try {
return require(`imagemin-${plugin}`).apply(null, args);
} catch (err) {
gutil.log(`gulp-imagemin: Couldn't load default plugin "${plugin}"`);
}
};
If log error in catch, we can see Module not found xxx
.
try {
return require(`imagemin-${plugin}`).apply(null, args);
} catch (err) {
console.log(err); // and will throw error: XX Module Not Found
gutil.log(`gulp-imagemin: Couldn't load default plugin "${plugin}"`);
}
So we could install the lost packages.
If also has some error, just reinstall imagemin
npm uninstall gulp-imagemin --save
npm install gulp-imagemin --save
回答2:
Looks like this is a node
6 problem. Try downgrading the version you're using for this project. I get gifsicle install errors on 6.9.5 but not on 6.3.1 (haven't tried all the versions in between).
If you aren't familiar with switching between versions of node, the recommended solution is to use nvm
.
Read through the notes in https://github.com/creationix/nvm#installation (not all OSes are supported), and then run
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
The nvm
documentation is thorough, but basically you're going to run nvm install YOURVERSIONNUMBER
and then nvm use YOURVERSIONNUMBER
. (Also worth checking out: how to use .nvmrc, and how to automatically load your project's version of node.)
来源:https://stackoverflow.com/questions/42152008/gulp-imagemin-couldnt-load-default-plugin-xxx