Mikro ORM bundle error
Unanswered
Borzoi posted this in #help-forum
BorzoiOP
Hi. I'm trying to implement Mikro ORM into my stack, but get this bundle error (webpack?)
What do I do?
What do I do?
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>
Import trace for requested module:
../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
../../node_modules/.pnpm/sqlite3@5.1.6/node_modules/sqlite3/lib/sqlite3-binding.js
../../node_modules/.pnpm/sqlite3@5.1.6/node_modules/sqlite3/lib/sqlite3.js
../../node_modules/.pnpm/knex@2.5.1_sqlite3@5.1.6/node_modules/knex/lib/dialects/sqlite3/index.js
../../node_modules/.pnpm/@mikro-orm+knex@5.7.14_@mikro-orm+core@5.7.14_@mikro-orm+migrations@5.7.14_sqlite3@5.1.6/node_modules/@mikro-orm/knex/MonkeyPatchable.js
../../node_modules/.pnpm/@mikro-orm+knex@5.7.14_@mikro-orm+core@5.7.14_@mikro-orm+migrations@5.7.14_sqlite3@5.1.6/node_modules/@mikro-orm/knex/index.js
../../node_modules/.pnpm/@mikro-orm+sqlite@5.7.14_@mikro-orm+core@5.7.14_@mikro-orm+migrations@5.7.14_@mikro-orm+seeder@5.7.14/node_modules/@mikro-orm/sqlite/index.js
../../node_modules/.pnpm/@mikro-orm+sqlite@5.7.14_@mikro-orm+core@5.7.14_@mikro-orm+migrations@5.7.14_@mikro-orm+seeder@5.7.14/node_modules/@mikro-orm/sqlite/index.mjs
./src/utils/db.ts
./src/server/index.ts
./src/app/_trpc/serverClient.ts
./src/app/page.tsx10 Replies
Abyssinian
Hi there,
The error you're seeing is indeed a Webpack error, and it's typically related to an imported module (in this case, related to node-pre-gyp and subsequently, sqlite3) which seems to be trying to include an HTML file. This is something Webpack doesn't know how to handle by default.
Here's how to tackle the issue:
Server-side Only Code:
The most important thing you need to realize is that libraries like sqlite3 should not be bundled with the frontend code because they are meant to be used server-side only. So, the first thing to do is make sure you're not importing or using them in any client-side code. You mentioned ./src/app/page.tsx in your import trace - ensure that you're not directly or indirectly importing server-side modules here.
Webpack Externals:
If you are sure that you're using the sqlite3 library server-side only, you can tell Webpack not to bundle it (and related libraries) by using the externals configuration. With Next.js, you can modify your next.config.js:
This configuration tells Webpack not to bundle these modules when compiling the server-side code.
Dynamic Imports:
Another tactic is to use dynamic imports with next/dynamic for code paths that might touch server-only code. This way, they won't be included in the initial client-side bundle.
Optional:
There seems to be an issue with node-pre-gyp trying to load an HTML file, which is odd for server-side libraries. Consider checking the versions you're using and see if there's a newer version without this problem. If not, it might be worth checking with the library maintainers.
Lastly, always ensure you're segregating your server and client logic. Directly importing server-side logic into your components will lead to these types of issues.
The error you're seeing is indeed a Webpack error, and it's typically related to an imported module (in this case, related to node-pre-gyp and subsequently, sqlite3) which seems to be trying to include an HTML file. This is something Webpack doesn't know how to handle by default.
Here's how to tackle the issue:
Server-side Only Code:
The most important thing you need to realize is that libraries like sqlite3 should not be bundled with the frontend code because they are meant to be used server-side only. So, the first thing to do is make sure you're not importing or using them in any client-side code. You mentioned ./src/app/page.tsx in your import trace - ensure that you're not directly or indirectly importing server-side modules here.
Webpack Externals:
If you are sure that you're using the sqlite3 library server-side only, you can tell Webpack not to bundle it (and related libraries) by using the externals configuration. With Next.js, you can modify your next.config.js:
module.exports = {
webpack: (config, { isServer }) => {
if (isServer) {
config.externals = ['sqlite3', '@mapbox/node-pre-gyp', ...config.externals];
}
return config;
},
};This configuration tells Webpack not to bundle these modules when compiling the server-side code.
Dynamic Imports:
Another tactic is to use dynamic imports with next/dynamic for code paths that might touch server-only code. This way, they won't be included in the initial client-side bundle.
Optional:
There seems to be an issue with node-pre-gyp trying to load an HTML file, which is odd for server-side libraries. Consider checking the versions you're using and see if there's a newer version without this problem. If not, it might be worth checking with the library maintainers.
Lastly, always ensure you're segregating your server and client logic. Directly importing server-side logic into your components will lead to these types of issues.
BorzoiOP
Hi @Abyssinian, great answer.
The
This is a server component, while the
The
src/app/page.tsx file looks as follows:import TodoList from '@/app/_components/TodoList'
import { serverClient } from '@/app/_trpc/serverClient'
const Home = async (): Promise<JSX.Element> => {
const todos = await serverClient.getTodos()
return (
<main className="max-w-3xl mx-auto mt-5">
<TodoList initialTodos={todos} />
</main>
)
}
export default HomeThis is a server component, while the
TodoList is the client component which has 'use client' and uses the TRPC client library (with react query) and a hook to fetch the data from the TRPC api route.So it should be bundling that only in the server code. At least, that's the plan.
Is there any way to check if the error is generated by the server or the client bundle?
Abyssinian
Ah, I see. If 'Home' is intended to be a server component, then there are some issues in the way you've structured it.
Server components should not directly run side-effects like fetching data within the component body. The fetching should be done outside the component.
Here's a refactored version that works more in line with the Next.js data fetching method:
Server components should not directly run side-effects like fetching data within the component body. The fetching should be done outside the component.
Here's a refactored version that works more in line with the Next.js data fetching method:
tsx
import TodoList from '@/app/_components/TodoList'
import { serverClient } from '@/app/_trpc/serverClient'
const Home = ({ todos }): JSX.Element => {
return (
<main className="max-w-3xl mx-auto mt-5">
<TodoList initialTodos={todos} />
</main>
)
}
export async function getServerSideProps() {
const todos = await serverClient.getTodos();
return {
props: {
todos,
}
}
}
export default HomeIn this version, the data-fetching is moved to the getServerSideProps function, which is Next.js's way to fetch data server-side and pass it as props to the component. The component itself remains pure and only handles rendering.
This ensures that the side effect of fetching todos is performed on the server-side only and will not be bundled into the client-side code. This should prevent any client-side bundling of server-only code.
This ensures that the side effect of fetching todos is performed on the server-side only and will not be bundled into the client-side code. This should prevent any client-side bundling of server-only code.
BorzoiOP
I'm using Next.js version 13 which allows async data fetching in server side components
And there's no more getServerSideProps in Next 13
Abyssinian
hmmm. I'm very new to NextJS 13 as well (I have a post above too), so I'm not exactly sure on this one. But I do know that you can distinguish between client and server-side errors:
1. Console Output:
In next build or next dev, errors after Compiling server... are server-side; after Compiling client... are client-side.
2. .next Folder:
Errors in .next/server are server-side.
Errors in .next/static are client-side.
3. Isolate Builds:
Modify next.config.js to skip server or client bundling temporarily to identify error origin.
The error's context during build can hint at whether it's server or client-side. Ensure server-side modules are imported only in server-side code.
1. Console Output:
In next build or next dev, errors after Compiling server... are server-side; after Compiling client... are client-side.
2. .next Folder:
Errors in .next/server are server-side.
Errors in .next/static are client-side.
3. Isolate Builds:
Modify next.config.js to skip server or client bundling temporarily to identify error origin.
The error's context during build can hint at whether it's server or client-side. Ensure server-side modules are imported only in server-side code.