当前位置: 首页 > 知识库问答 >
问题:

使用Webpack和Babel“您可能需要一个适当的加载器来处理此文件类型”

姬俊能
2023-03-14

我正在尝试使用带有Babel的Webpack来编译ES6资产,但是我得到了以下错误消息:

You may need an appropriate loader to handle this file type.
| import React from 'react';
| /*
| import { render } from 'react-dom'
var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: './index',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/dist/'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var config = require('./webpack.config');

var express = require('express');
var app = express();
var port = 3000;

var compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
  noInfo: true,
  publicPath: config.output.publicPath
}));

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.listen(port, function(err) {
  console.log('Server started on http://localhost:%s', port);
});

我的index.js文件所做的只是导入react,但看起来'babel-loader'不起作用。

我正在使用“babel-loader”6.0.0。

共有1个答案

仲君昊
2023-03-14

您需要安装ES2015预置:

npm install babel-preset-es2015

然后配置babel-loader:

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    exclude: /node_modules/,
    query: {
        presets: ['es2015']
    }
}
 类似资料: