我正在尝试使用webpack-dev-server设置react-hot-loader,但是我收到了这个错误:
ERROR in ./app/index.jsx
Module not found: Error: Can't resolve './components/App' in '/myURL/app'
我有另一个没有使用react-hot-module的webpack,而且我正在使用express服务器,所以我的代码没问题,问题是webpack配置。
这是我的webpack配置,其中包含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
},
};
所以我的index.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'));
我的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;
任何人都可以帮我吗?
提前致谢。
答案 0 :(得分:2)
假设您在目录结构下引用了正确的文件,我相信您错过了这个:
module.exports = {
resolve: {
extensions: ['.jsx', '.js']
}
};
https://webpack.js.org/configuration/resolve/#resolve-extensions