You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Monorepo项目中Webpack提示需额外Loader的问题解决

问题:React Monorepo跨工作区引用TSX组件解析失败

项目背景

我搭建了一个包含webmail和component两个工作区的React Monorepo项目,在component工作区编写了TSX组件,想要在webmail工作区中引用,但运行时出现解析错误。


Component工作区配置

package.json

{"name": "@monorepo/component","version": "0.1.0","description": "Creating a component module project","main": "src/index.tsx","scripts": {"dev": "webpack serve","build": "webpack"},"keywords": ["react","component","npm"],"author": "Christine","license": "MIT","peerDependencies": {"react": "^18.2.0","react-dom": "^18.2.0"},"devDependencies": {"@babel/core": "^7.24.0","@babel/preset-env": "^7.12.11","@babel/preset-react": "^7.18.6","@babel/register": "^7.12.10","babel-loader": "^9.1.3","html-webpack-plugin": "^5.6.0","webpack": "^5.90.3","webpack-cli": "^5.1.4"},"dependencies": {"react": "^18.2.0","react-dom": "^18.2.0"}}

webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        index: { import: "./src/index.tsx" }
    },
    output: {
        path: path.resolve(__dirname, "dist"),
    },
    mode: "production",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: [
                            '@babel/preset-env',
                            '@babel/preset-react'
                        ]
                    }
                }
            }
        ]
    },
}

TSX组件代码

import React from "react-dom";

interface ComponentProps {
   label: string
}

export const MyComponent=({label, ...props}: ComponentProps)=> {

    return (
        <div>
            <button>{label}</button>
        </div>
    );
}

Webmail工作区引用代码

import './css/App.css';
import React from 'react'
import { MyComponent } from "@monorepo/component"
function App() {

   return (
        <div className="flex flex-col h-screen w-screen">
            <nav className="nav">
                <div className='flex-wrap flex justify-between w-4/6'>
                    <div className='flex-wrap flex '>
              <MyComponent label='my button'/>

                    </div>
                   
                </div>
            </nav>
         </div>
    );
}

export default App;

报错信息

运行webmail项目时首次报错:

ERROR in ../component/src/index.tsx 4:0
Module parse failed: The keyword 'interface' is reserved (4:0)
File was processed with these loaders:
 * ../node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
| import React from "react-dom";
> interface ComponentProps {
|    label: string
| }

测试调整后的报错

  • 移除组件代码中的interface,报错变为:
Module parse failed: Unexpected token (9:8)
File was processed with these loaders:
 * ../node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
|     return (
>         <div>
|             <button>{label}</button>
|         </div>
ERROR in ../component/src/index.tsx 9:8
  • 将component中的index.tsx改为index.js,报错变为:
Add @babel/preset-react to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx to the 'plugins' section to enable parsing.

我尝试在package.json、webpack.config.json、babelrc.json中调整Babel预设配置,但都没有效果,请问问题出在哪里?


解决方案

重新搭建项目后仍遇到相同问题,最终通过以下两步解决:

  1. 在component工作区添加tsconfig.json配置:
{"compilerOptions": {"target": "ES6","module": "ES2015","outDir": "dist","allowSyntheticDefaultImports": true,"jsx": "react"},"include": ["src/**/*.tsx"],"exclude": ["node_modules"]}
  1. 为Babel添加@babel/preset-typescript预设

内容的提问来源于stack exchange,提问作者Christine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:35:00