Cannot read property 'main' of undefined

2019-06-16 03:24发布

问题:

So my project structures is I have a src and www directory in my root ./, which also contains my bower.json, gulpfile.js, and .bowerrc with the directory set to ./www/bower/.

I have an index.html in my ./src and I've setup a gulp task that pipes it through wiredep and out to the ./www where the bower packages are.

Unfortunately it adds all the dependecies as if it's in the ./src directory so all of them are prefixed like ../www/bower/ which does work as the final index.html ends up in the www directory so I fiddled with some of the wiredep configuration options like so:

gulp.task('bower', function () {
    gulp.src('./src/index.html')
    .pipe(wiredep({
        cwd: './www',
        bowerJson: require('./bower.json'),
        directory: '../.bowerrc'
    }))
    .pipe(gulp.dest('./www'));
});

However I get the following error:

stream.js:94
    throw er; // Unhandled stream error in pipe.
            ^
TypeError: Cannot read property 'main' of undefined
    at findMainFiles (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\lib\detect-dependencies.js:53:37)
    at D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\lib\detect-dependencies.js:111:17
    at forOwn (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\node_modules\lodash\dist\lodash.js:1301:15)
    at Function.forEach (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\node_modules\lodash\dist\lodash.js:2595:9)
    at detect (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\lib\detect-dependencies.js:312:5)
    at wiredep (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\wiredep.js:178:39)
    at Transform._transform (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\wiredep.js:217:34)
    at Transform._read (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at Transform._write (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (D:\Dropbox\University\MindFlipDOM\node_modules\wiredep\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:238:10)

So what am I doing wrong?

回答1:

Also try, this will make sure to download the necessary modules that were missing.

bower install


回答2:

Well as far as I can guess, you messed up with bower. Most likely you uninstalled a dependency and forgot to save.

What you should have done:

bower uninstall <dependency> --save

What you probably did:

bower uninstall <dependency>

You can solve this issue by bower executing uninstall <dependency> --save or if you are unsure of which components you uninstalled, you can edit bower.json file and remove components that are not installed. (You can check if a dependency is installed in bower_componenets directory)