找不到模块:错误:无法解析“主页”

时间:2020-04-19 17:40:06

标签: javascript reactjs webpack webpack-4

我刚刚开始学习React,在尝试使用Webpack进行构建时偶然发现了这个错误。

https://metacpan.org/

我的webpack.config.js:

Module not found: Error: Can't resolve 'Home' in 'D:\wamp64\www\Wallet\src\components'
resolve 'Home' in 'D:\wamp64\www\Wallet\src\components'

我的Home.js:

const path = require('path');
module.exports = {
  entry: {
    index: '.src/index.js',
    js: './src/js/index2.js',
    react: './src/components/App.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader',
        query: {
          presets: ['env', 'stage-0', 'react']
        }
      }
    ]
  }
};

这是我在App.js中导入Home的方式:


const Home = () => {
  return <h1>Home</h1>
}

export default Home

我在这里遇到了几个问题,这些问题建议在src文件夹中添加一个空的index.js,但这也行不通。

1 个答案:

答案 0 :(得分:1)

您的导入不正确

import Home from 'Home';
import About from 'About';

您需要指定文件的路径和路径,例如:

import Home from './src/components/Home.js';

home.js是您导出组件的文件

相关问题