Webpack错误:“您可能需要一个合适的加载程序来处理此文件类型”

时间:2016-03-28 10:58:49

标签: javascript reactjs webpack babeljs

我在使用Webpack编译ES6和JSX文件时遇到以下错误“您可能需要适当的加载程序来处理此文件类型”。

终端错误:

You may need an appropriate loader to handle this file type.
| import React from 'react';
| import ReactDOM from 'react-dom';

webpack.config.js 看起来像这样,

module.exports = {
    entry: './main.js',
    output: {
        path: './',
        filename: 'index.js'
    },
    devServer: {
        inline: true,
        port: 2525
    },
    module: {
        loaders: [
            {
                test: '/\.js$/',
                exclude: /node_modules/,
                loader: 'babel-loader'
                query: {
                    presets: ['es2015','react']
                }
            }
        ]
    }
}

main.js 文件包含,

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

class App extends React.Component {
    render(){
        return <h1>Title text</h1>
    }
}
ReactDOM.render(<App/>,document.getElementById('app'));

package.json 看起来像这样,

{
  "name": "react-webpack-es6",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^0.14.7",
    "react-dom": "^0.14.7"
  },
  "devDependencies": {
    "babel-core": "^6.7.4",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  }
}

如果我在这里遗漏了一些东西,请告诉我。

1 个答案:

答案 0 :(得分:2)

webpack.config.js中,test中的module.loaders属性应该是正则表达式,而不是字符串。

module: {
  loaders: [{
    test: '/\.js$/',

将此'/\.js$/'更改为此/\.js$/

module: {
  loaders: [{
    test: /\.js$/,

了解更多信息,请访问webpack/module-loaders