0
votes

I am trying to setup react-hot-loader with webpack-dev-server but I got this error:

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

I have another webpack working with no react-hot-module and I'm using with the express server, so my code is ok and the problem is the webpack configuration.

Here is my webpack config with react-hot-loader:

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
  },
};

So my index.jsx is:

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'));

And my App.jsx is:

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;

Can anyone help me with this?

Thanks in advance.

1

1 Answers

3
votes

Assuming you have referenced the correct file under your directory structure, I believe that you have missed this:

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

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