反应热加载程序 3 - 未找到模块



我正在尝试使用 webpack-dev-server 设置 react-hot-loader,但我收到此错误:

ERROR in ./app/index.jsx
Module not found: Error: Can't resolve './components/App' in '/myURL/app'

有另一个没有反应热模块的 webpack,我正在使用快速服务器,所以我的代码没问题,问题是 webpack 配置。

这是我使用 react-hot-loader 的 webpack 配置:

const path = require('path');
const webpack = require('webpack');
const SassLintPlugin = require('sasslint-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const extractSass = new ExtractTextPlugin({
  filename: '[name].[contenthash].css',
  disable: process.env.NODE_ENV === 'development',
});
module.exports = {
  entry: [
    'react-hot-loader/patch',
    // activate HMR for React
    'webpack-dev-server/client?http://localhost:3000',
    // bundle the client for webpack-dev-server
    // and connect to the provided endpoint
    'webpack/hot/only-dev-server',
    // bundle the client for hot reloading
    // only- means to only hot reload for successful updates
    './app/index.jsx',
    // the entry point of our app
  ],
  output: {
    filename: '[name].js',
    // the output bundle
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/static/',
    // necessary for HMR to know where to load the hot update chunks
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /.jsx?$/,
        include: path.resolve(__dirname, './app/'),
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /.scss$/,
        include: path.resolve(__dirname, './app/'),
        loader: extractSass.extract({
          use: [
            { loader: 'css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]' },
            { loader: 'sass-loader' },
          ],
          // use style-loader in development
          fallback: 'style-loader',
        }),
        exclude: /node_modules/,
      },
      {
        test: /.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=fonts/[name].[ext]',
      },
    ],
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    // enable HMR globally
    new webpack.NamedModulesPlugin(),
    // prints more readable module names in the browser console on HMR updates
    new webpack.NoEmitOnErrorsPlugin(),
    // do not emit compiled assets that include errors
    new SassLintPlugin({
      configFile: '.sass-lint.yml',
      context: [path.resolve(__dirname, './app/')],
      ignoreFiles: [],
      ignorePlugins: [],
      glob: '**/*.s?(a|c)ss',
      quiet: false,
      failOnWarning: false,
      failOnError: true,
      testing: false,
    }),
    extractSass,
    new HtmlWebpackPlugin({
      title: 'Contactto Master',
      template: 'app/index-template.html',
      minify: {
        collapseWhitespace: process.env.NODE_ENV === 'production',
      },
    }),
  ],
  devServer: {
    host: 'localhost',
    port: 3000,
    historyApiFallback: true,
    // respond to 404s with index.html
    hot: true,
    // enable HMR on the server
  },
};

所以我的索引.jsx是:

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './components/App';
render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
document.getElementById('app'));

我的应用程序.jsx是:

import React from 'react';
import Header from './header/Header';
import Main from './main/Main';
import Sidebar from './sidebar/Sidebar';
import style from './App.scss';
const App = () => (
  <div className={style.app}>
    <Header />
    <Sidebar />
    <Main />
  </div>
);
export default App;

谁能帮我解决这个问题?

提前谢谢。

假设您在目录结构下引用了正确的文件,我相信您错过了:

module.exports = {
  resolve: {
    extensions: ['.jsx', '.js']
  }
};

https://webpack.js.org/configuration/resolve/#resolve-extensions

最新更新