I would like to import a simple mui component.
My component:
import Button from "@mui/material/Button";
export default function Bar() {
return <Button variant="contained">Contained</Button>;
};
I use vite (with rolldown-vite) to build this component:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
define: {
"process.env.NODE_ENV": JSON.stringify("production")
},
build: {
lib: {
entry: "src/Index.tsx",
name: "mycomponent",
formats: ["es"]
},
},
external: [
"react",
"@mui/material",
"@emotion/react",
"@emotion/styled"
],
output: {
globals: {
react: "React",
"@mui/material": "MaterialUI",
"@emotion/react": "EmotionReact",
"@emotion/styled": "EmotionStyled",
},
},
})
I use an http server to serve bundled js file. I can reach it with curl
When importing it with:
export const Plugins = ({ url}: { url: string }) => {
const [Comp, setComponent] = useState<FC>();
import(url)
.then((component) => setComponent(() => component.default))
.catch((err: unknown) => {
console.log(err);
});
return (
<Comp />
);
I get
(new TypeError("Wrong module specifier", ""))
%s
%s
I don't install react and mui deps into component because I installed them into my main app
I don't know why I get this issue.
I do something wrong during build / import ?
Update: I try to import it without lazy, get same issue
Update2: I added react and all @mui dependencies into my lib dependencies. I loaded my component:
import Bar from "http://localhost:8012/plugins/test/Foo.js"
return (
<Bar />
);
Now I get
(new TypeError("can't access property \"useContext\", w.H is null", "http://localhost:8012/plugins/test/foo.js", 266))
%s
%s
An error occurred in the <ForwardRef> component. Consider adding an error boundary to your tree to customize error handling behavior.
[2025-10-27T11:44:31Z ERROR script::dom::console] (new TypeError("can't access property \"useContext\", w.H is null", "http://localhost:8012/plugins/test/foo.js", 266))
Update3: I tried without Lazy / Suspense, still get
(new TypeError("Wrong module specifier", ""))
Update4: I installed my component into package.json, import default function and rendered the function. It works as expected. But this is exactly what I tried to avoid. My goal is to load my component from fs or served through an http server (Like I do currently)
Update5: I simplify my component:
function Bar() {
return (
<h1>hello from Bar</h1>
);
}
export default Bar;
I can successfully import it. So I guess issue is from mui