then 不是函数

then is not a function

我 运行 我的 node.js 项目中的以下代码。我需要 "imagemin" 和 "imageminGifsicle"。我收到以下错误

}).then(files => { ^ TypeError: imagemin(...).then is not a function

我以前没有在我的项目中使用过promises吗?我需要包含它们才能使用 .then 吗?

imagemin(['export/sample/out.gif'], 'export/sample/out2.gif', {
 plugins: [
  imageminGifsicle({optimizationLevel: 1})
 ]
}).then(files => {
  console.log(files);
  console.log("finished");
}).catch(err => {
  console.log("ERR:"+err);
  throw err;
});

.then 与 Promise 相关并且 imagemin added this only on 5.0.0.

您使用的是什么版本?

嗯,是的 - 您可以选择您喜欢的 promise 库,并使用 "then" 函数和 promise。在这种情况下,您使用的是 imagemin:

const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');

imagemin on GitHub

要使用 Promises,您可以阅读 MDN and here on Whosebug

上的一些规范和信息