`fs-extra` integrate with `bluebird ` got `Cannot read property 'then' of undefined` error

1.2k views Asked by At

Here is my code

var Promise = require('bluebird');
var fse = Promise.promisifyAll(require('fs-extra'));

fse.remove('./myDir').then(function () {
  console.log('Remove myDir done.')
});

I always get TypeError: Cannot read property 'then' of undefined error.

Version:

node: v6.9.2
bluebird: 3.4.7
fs-extra: 1.0.0

I searched and found a similar question but not exactly quite the same, and I tried that answer, unfortunately, it can't solve my problem.

Did I miss anything?

1

There are 1 answers

0
Chen Dachao On BEST ANSWER

I found the correct way is fse.removeAsync, add Asyncsuffix to fse.remove, please see bluebird API here. Hope it can help others.

Update:

With the latest fs-extra, I don't need to import bluebird anymore. See here, below syntax works well.

// Promise Usage
fs.remove('/tmp/myfile')
.then(() => {
  console.log('success!')
})
.catch(err => {
  console.error(err)
})