简体   繁体   English

得到错误 Module not found: Error: Can't resolve '../components/***' when webpack build, 我该如何解决这个问题?

[英]got error Module not found: Error: Can't resolve '../components/***' when webpack build,how can I solve this problem?

in my code like this:在我的代码中是这样的:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import React, { Suspense, lazy } from 'react'
import Header from '../components/Header'
import Left from '../components/Left'
import Footer from '../components/Footer'

const Home = lazy(() => import('../pages/home'))

const Routes = () => (
  <Router>
    <div>
      <Header />
      <aside className="g-left-menu">
        <Left />
      </aside>
      <main className="g-page-content">
        <Suspense fallback={<div>loading...</div>}>
          <Switch>
            <Route exact path="/" component={Home}/>
          </Switch>
        </Suspense>
      </main>
      <Footer />
    </div>
  </Router>
)
export default Routes

In my package.json:在我的 package.json 中:

    "scripts": {
        "build": "cross-env NODE_ENV=development webpack --mode development",
    }

when I run npm run build:当我运行 npm run build 时:

Module not found: Error: Can't resolve '../components/Footer'
Module not found: Error: Can't resolve '../components/Header'
Module not found: Error: Can't resolve '../components/Leftmenu'

In my webpack.config.js在我的 webpack.config.js

resolve: {
    extensions: [ '.jsx', '.js' ],
    modules: [
        path.join(__dirname, 'node_modules'),
        path.resolve(__dirname, 'client'),
    ],
    ...
 }

enter image description here but when I run npm run build ,I got error: enter image description here在此处输入图像描述,但是当我运行 npm run build 时,出现错误:在此处输入图像描述

and my files dir like this: enter image description here和我的文件目录是这样的:在此处输入图像描述

I think your routes folder is inside the components folder if I interpreted the image right.如果我正确解释图像,我认为您的路由文件夹位于 components 文件夹内。 Assuming the code you are trying to show here is router/index.jsx .假设您要在此处显示的代码是router/index.jsx then, your imports should be "../Footer".然后,您的导入应该是“../Footer”。

Also, just a heads up, I'm not sure whether or not .tsx will cause a problem.另外,请注意,我不确定.tsx是否会导致问题。 I suggest if you're going typescript, then do it all the way and for the sake of consistency, though entirely up to you.我建议如果你要打字稿,那么为了一致性起见,一直这样做,尽管完全取决于你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“module not found can't resolve 'Api Client'”错误? - How to solve “module not found can't resolve 'Api Client'” error? 找不到模块:错误:无法解析“ @ cycle / run” webpack 2.2.1 - Module not found: Error: Can't resolve '@cycle/run' webpack 2.2.1 webpack-找不到模块:错误:无法解析&#39;jquery&#39; - webpack - Module not found: Error: Can't resolve 'jquery' webpack:找不到模块:错误:无法解析(使用相对路径) - webpack: Module not found: Error: Can't resolve (with relative path) Webpack:找不到模块:错误:无法解析“./containers” - Webpack: Module not found: Error: Can't resolve './containers' Webpack“找不到模块:错误:无法解析'./src/'” - Webpack "Module not found: Error: Can't resolve './src/'" 如何解决找不到模块错误:找不到模块:无法解析“@mui/icons-material/Adb”? - How to solve module not found error : Module not found: Can't resolve '@mui/icons-material/Adb'? `npx webpack-dev-server` 时 webpack-dev-server 引发“未找到模块:错误:无法解析” - webpack-dev-server raise “Module not found: Error: Can't resolve” when `npx webpack-dev-server` Webpack:移动 webpack/react 文件的位置后,npm start “Module not found: Error: Can&#39;t resolve ...” - Webpack: npm start "Module not found: Error: Can't resolve ..." after moving location for webpack / react files 这是我遇到了一段时间的错误“找不到模块:无法解析'../lib'”,不知道问题出在哪里 - This is an error i have been encountering for a while now "Module not found: Can't resolve '../lib'", no idea what the problem is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM