snorkelzebra
snorkelzebra

Reputation: 663

Webpack error: You may need an appropriate loader to handle this file type

I am new to webpack and react and am trying to set up an application. From looking at previous questions I think there must be something wrong with how I am setting up babel-loader, but I'm unable to see what my mistake is. Is anyone else able to see it?

webpack.config.js:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');

var config = {
  entry: APP_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module: [
  {
     test: /\.jsx?/,
     include: APP_DIR,
     loaders: ["babel-loader"],
     query: {
       presets: ['es2015', 'react']
     }
  }
  ]
};

module.exports = config;

babel.rc

{
	"presets": ["es2015", "react"]
}

Index.jsx

import React from 'react';
import ReactDom from 'react-dom';

class App extends React.Component {

	render() {
		return <p>Hello React!</p>;
	}
}

ReactDom.render(<App />, document.getElementById('app'));

Upvotes: 1

Views: 1081

Answers (2)

Shubham Khatri
Shubham Khatri

Reputation: 282120

Change you webpack file to include the babel-loader within quotes and included in an array of loaders as shown below. In modules you define an array of loaders to handle different types of files but a single loader for a particular type.

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');

var config = {
  entry: APP_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
   module: {
    loaders: [
      {
        test: /\.js?$/,
        exclude: /(node_modules|bower_components)/,
        include: APP_DIR,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015'],
        }
      }
    ]
  },
};

module.exports = config;

Upvotes: 0

dting
dting

Reputation: 39307

Here is the documentation for the module options object: https://webpack.github.io/docs/configuration.html#module

If you have babel-preset-2015 and babel-preset-react npm modules installed and the following webpack.config.js (babel.rc file isn't needed with the query presets) should work:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');

var config = {
  entry: APP_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: /\.jsx?/,
      include: APP_DIR,
      loader: "babel-loader",
      query: {
        presets: ['es2015', 'react']
      }
    }]
  }
};

module.exports = config;

Upvotes: 1

Related Questions