I created a React project with two workspaces, ‘webmail’ and ‘component’. I put a small tsx file in ‘component’, I want to use that in ‘webmail’. This is the package.json of component:
{
"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"
}
}
This is webpack.config.js in component
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'
]
}
}
}
]
},
}
This is ‘MyComponent’:
import React from "react-dom";
interface ComponentProps {
label: string
}
export const MyComponent=({label, ...props}: ComponentProps)=> {
return (
<div>
<button>{label}</button>
</div>
);
}
This is the tsx file that uses MyComponent:
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;
What happens is that the webmail app launches, then complains about
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
| }
Edit:
If I remove the ‘interface’ in the component code, the error I get is
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
If I change the ‘index.tsx’ in the component to ‘index.js’, I get
Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/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 (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.
Somehow, the react preset for babel isn’t picked up, I think. I have tried putting the preset line in package.json or in webpack.config.json or in babelrc.json. Is there anything wrong in my configs?
Solution
I used this as an example for my project. I now recreated that example locally, it gave me the same error that I had for a tsx component. After putting this
{
"compilerOptions": {
"target": "ES6",
"module": "ES2015",
"outDir": "dist",
"allowSyntheticDefaultImports": true,
"jsx": "react"
},
"include": [
"src/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
in tsconfig.js, and adding the preset-typescript line suggested in two answers, the problem was solved, or so it seems for now.
2
Answers
I had a kind of same issue I fixed like this.
webpack config:
For .babelrc file:
I see two clear issues, both related to your component workspace.
Add
@babel/preset-typescript
to your webpack configuration to remove the TypeScript syntax.Update the
main
field in the package.json to point to the webpack build’soutput
location.Now make sure you build the component workspace before running the webmail workspace.
You may want/need to place the preset configurations inside of a babel.config.json file:
There may be other issues with how you are trying to build and run your webmail workspace, but it is hard to say without more information on how it is configured and how you are running that workspace.