3

尝试使用 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)

帮助表示感谢

4

1 回答 1

8

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

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

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

文档中的更多信息。

于 2019-02-28T17:04:44.940 回答