【问题标题】:Webpack: ERROR in ./src/index.tsx Module not found: Error: Can't resolve './App' in '...'Webpack:./src/index.tsx 中的错误模块未找到:错误:无法解析“...”中的“./App”
【发布时间】:2019-07-22 12:05:57
【问题描述】:

尝试使用 webpack-dev-server 为我的应用程序提供服务时出现以下错误:

ERROR in ./src/index.tsx
Module not found: Error: Can't resolve './App' in 
'C:\Users\user\Desktop\react-resources-boilerplate\src'
 @ ./src/index.tsx 15:28-44
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.tsx

这是我的 webpack 配置文件:

const HtmlWebPackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  entry: path.resolve(__dirname, 'src', 'index.tsx'),
  module: {
    rules: [
      {
        test: /\.ts|\.tsx$/,
        loader: "ts-loader",
      },
    ]
  },

  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

这是我的 tsconfig 文件:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "jsx": "react",
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true
  }
}

这是我的 index.tsx 文件:

import * as React from 'react';
import { render } from 'react-dom';
import App from './App';

const root = document.querySelector('#root')
render(<App />, root)

帮助表示赞赏 谢谢

【问题讨论】:

  • 你的文件结构是什么样的?你确定你的App文件和索引文件在同一个目录吗?
  • 是的,它也在 src 目录中。
  • 如果我在 ./src/components 中设置 App 组件,我会收到以下错误:ERROR in ./src/index.tsx Module not found: Error: Can't resolve './components/App' in 'C:\Users\user\Desktop\react-resources-boilerplate\src' @ ./src/index.tsx 15:28-55 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.tsx

标签: reactjs webpack ts-loader tsx


【解决方案1】:

将以下内容添加到您的 webpack 配置中:

module.exports = {
    // ...
    // existing code goes here
    // ...

    resolve: {
        extensions: ['.ts', '.tsx'],
    },
};

更多信息请关注docs。

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 2018-06-12
    • 2019-01-19
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多