Error: Cannot resolve ‘file’ or ‘directory’

If you just tried to add a new require or import to your React project (that’s using webpack + babel-loader) trying to pull in a new component (say search.jsx — note specifically the jsx extension) and are getting an error that looks something like the following:

[code]
Error: Cannot resolve ‘file’ or ‘directory’
[/code]

…chances are, that you need to tell webpack to resolve the jsx extension:

[code]
module.exports = {
// …
resolve: {
extensions: [ ”, ‘.js’, ‘.jsx’ ];
}
// …
}
[/code]

Note that you may need others depending on the loaders you have.

Leave a Reply