Npm Run Build causes issue Element type is invalid because of importing SVG element?
Unanswered
Little Shearwater posted this in #help-forum
Little ShearwaterOP
Error occurred prerendering page "/sign-up". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Looking into this issue, it turns out that this usage of SVG element causes the issue (in any component that uses SVG element)
import { avenirBold } from "@/Utilities/Exports/Fonts";
import TextLabel from "../Decorations/Text";
import RobloxLogo from "@/public/SVG Icons/roblox-logo.svg";
interface RobloxAuthButtonProps {
className: string
onClick: () => void
}
export default function RobloxAuthButton(props: RobloxAuthButtonProps) {
return (
<button
className={`bg-black pl-4 pr-4 flex items-center rounded-lg hover:bg-gray-900 active:bg-gray-800 hover:cursor-pointer ${props.className}`}
onClick={props.onClick}
>
<RobloxLogo width={32} height={32}/>
<div className="flex-1 mr-8">
<TextLabel className={`${avenirBold.className} text-white`}>Continue with Roblox</TextLabel>
</div>
</button>
)
}
This is one of them.
32 Replies
Little ShearwaterOP
I'm using
@svgr/webpack
Little ShearwaterOP
Further investigation:
I found out that importing SVG indeed return
I found out that importing SVG indeed return
object
My config:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
experimental: {
turbo: {
rules: {
"*.svg": {
loaders: ["@svgr/webpack"],
as: "*.js",
}
}
}
}
};
export default nextConfig;
## Note
This works in Development mode
This works in Development mode
It only occur when building in production
Little ShearwaterOP
# SOLUTION
Use this method (creating aReference: https://discord.com/channels/752553802359505017/752647196419031042/1335631895257546864IconSvg
component)
Thank you @Arinji
🙏 🙏 🙏
Yo wth? Lmao that's fucking awesome
@Little Shearwater It only occur when building in production
Pont-Audemer Spaniel
I came across the same issue. That's because
turbo
is used only for dev
. I fixed the issue by just adding webpack
config alongside turbo. Now it works fine with .svg
files and for both dev and prod@Pont-Audemer Spaniel I came across the same issue. That's because `turbo` is used only for `dev`. I fixed the issue by just adding `webpack` config alongside turbo. Now it works fine with `.svg` files and for both dev and prod
Little ShearwaterOP
Really? Interesting, shouldn't you get error like "You are using webpack while using turbo at same time"? I don't really remember but it happens when I do that.
Looks like I guess it's pretty much in beta?
@Little Shearwater Really? Interesting, shouldn't you get error like "You are using webpack while using turbo at same time"? I don't really remember but it happens when I do that.
Pont-Audemer Spaniel
No error, i'm running dev with --turbopack flag. Afaik it should just ignore webpack config when using the flag.
@Pont-Audemer Spaniel I came across the same issue. That's because `turbo` is used only for `dev`. I fixed the issue by just adding `webpack` config alongside turbo. Now it works fine with `.svg` files and for both dev and prod
Somali
I just saw this post. Can you show me your webpack config?
Nah now I got an error when running npm start:
Error: Expected to use Webpack bindings (react-server-dom-webpack/server.edge) for React but the current process is referencing '__esModule' from
the Turbopack bindings (react-server-dom-turbopack/server.edge). This is likely a bug in our integration of the Next.js server runtime.
Pont-Audemer Spaniel
webpack(config) {
config.module.rules.push({
test: /\.svg$/i,
use: [
{
loader: "@svgr/webpack",
options: {
dimensions: false,
},
},
],
});
return config;
},
experimental: {
mdxRs: true,
turbo: {
rules: {
"*.svg": {
loaders: [
{
loader: "@svgr/webpack",
options: {
dimensions: false,
},
},
],
as: "*.js",
},
},
},
},
Pretty much that's all
Somali
Do you use any build flags?
Pont-Audemer Spaniel
No, i do use "output: standalone" in next config but that's all. I'm on "next": "15.1.7"
Somali
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/i,
use: [
{
loader: "@svgr/webpack",
options: {
svgoConfig: require("./svgo.config.js"),
},
},
],
});
return config;
},
experimental: {
turbo: {
rules: {
"*.svg": {
loaders: ["@svgr/webpack"],
as: "*.ts",
options: require("./svgo.config.js"),
},
},
},
},
};
export default nextConfig;
That is my config rn. Is there a problem?
Pont-Audemer Spaniel
minor differences but i doubt it's related to the error you're getting.
Putting options like so didn't work for me, i had to do
But it does not seem like it's part of the issue you're having
"*.svg": {
loaders: ["@svgr/webpack"],
as: "*.ts",
options: require("./svgo.config.js"),
},
Putting options like so didn't work for me, i had to do
"*.svg": {
loaders: [{loader: "@svgr/webpack", options: require("./svgo.config.js")}],
as: "*.ts",,
},
But it does not seem like it's part of the issue you're having
Somali
I used standalone output and now it is working?
Hm I guess that was it.
Thank you very much! You saved me many many hours haha
Now I get the error "next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.
But when I use this my axios requests wont work? And when I use npm start everything seems fine? (besides the error) @Pont-Audemer Spaniel
But when I use this my axios requests wont work? And when I use npm start everything seems fine? (besides the error) @Pont-Audemer Spaniel
Somali
I think it has something to do with the .env?
Pont-Audemer Spaniel
I don't think using
Tbh i'm not sue why would this fix the issue you're having, but to me it does not look like a proper way to handle the issue. But i'm not sure what is the proper solution tho 😄
I might play around with this after work
output: standalone
is a correct solution if you don't actually want to do that. For example if you're deplyoing to vercel you shouldn't use that option, my guess is that it won't work.Tbh i'm not sue why would this fix the issue you're having, but to me it does not look like a proper way to handle the issue. But i'm not sure what is the proper solution tho 😄
I might play around with this after work
Somali
Okay maybe it isnt fixed then. 😦 I want to deploy it on a hetzner vps but idk if its a problem. When I cut off output: standalone I get the error back.
@Somali Okay maybe it isnt fixed then. 😦 I want to deploy it on a hetzner vps but idk if its a problem. When I cut off output: standalone I get the error back.
Pont-Audemer Spaniel
Hey, i just tested it without
standalone
and still everything works fine on my side. turbopack on dev and webpack on prod@Pont-Audemer Spaniel Hey, i just tested it without `standalone` and still everything works fine on my side. turbopack on dev and webpack on prod
Somali
GET http://localhost/_next/static/chunks/app/user/swipe/page-21d05fe6ac66c7ff.js net::ERR_ABORTED 404 (Not Found) is the error I get when using the node server.js. I use axios in this file.
Somali
I tested cutting of the .env but even when going to my home "/" with nox axios reuqests I get errors like this.
I removed standalone and now it builds fine. I dont know. But it is good enough for me I guess.