Open Neo-Ciber94 opened 6 months ago
I found out nextjs is unable to resolve the location of @electric-sql/pglite
maybe because is a monorepo.
Adding a loader that transpile the package its working for me
/** @type {import('next').NextConfig} */
let nextConfig = {
images: {},
webpack: (config, options) => {
config.module.rules.push({
test: /\.+(js|jsx|mjs|ts|tsx)$/,
use: options.defaultLoaders.babel,
include: fileURLToPath(import.meta.resolve("@electric-sql/pglite")),
type: "javascript/auto",
});
if (!options.isServer) {
config.resolve.fallback = { fs: false, module: false, path: false };
}
return config;
},
transpilePackages: [],
};
I found out nextjs is unable to resolve the location of
@electric-sql/pglite
maybe because is a monorepo.Adding a loader that transpile the package its working for me
/** @type {import('next').NextConfig} */ let nextConfig = { images: {}, webpack: (config, options) => { config.module.rules.push({ test: /\.+(js|jsx|mjs|ts|tsx)$/, use: options.defaultLoaders.babel, include: fileURLToPath(import.meta.resolve("@electric-sql/pglite")), type: "javascript/auto", }); if (!options.isServer) { config.resolve.fallback = { fs: false, module: false, path: false }; } return config; }, transpilePackages: [], };
I tried this and worked in dev but not building. same error. any ideas?
@prananta
I use pglite-react
and pglite-repl
.
The solution above didn't work.
I am able to fix the build error by adding these options to next-config.js
const nextConfig = {
swcMinify: false,
transpilePackages: [
'@electric-sql/pglite-react',
'@electric-sql/pglite-repl',
'@electric-sql/pglite',
],
}
Notes:
I must delete .next
folder before build or it will error again
rm -rf .next
If a similar error occurs, try removing the packages and add again
Thanks @zelief! I noticed I only needed the swcMinify: false
(i.e. it can work without transpilePackages
). FWIW, I use bun v1.1.21 with next v14.2.6.
It shows warning that this approach won't work in the next major version, but at least it works for now.
For me what helped was adding it as part of the experimental serverComponentsExternalPackages
...
experimental: {
serverComponentsExternalPackages: ['@electric-sql/pglite'],
},
...
Can confirm that this works with Nextjs 15 (from https://github.com/electric-sql/pglite/issues/322#issuecomment-2372563526):
// next.config.mjs
const nextConfig = {
serverExternalPackages: ["@electric-sql/pglite"],
};
export default nextConfig;
Link to the code that reproduces this issue
https://github.com/Neo-Ciber94/pglite-next-bug
To Reproduce
@electric-sql/pglite
page.tsx
with this query, that just query the time:import { PGlite } from "@electric-sql/pglite"; import { useEffect, useRef, useState } from "react";
export default function HomePage() { const db = useRef();
const [data, setData] = useState("PENDING");
useEffect(() => { if (db.current) { return; }
}, []);
return (
PGlite
); }
npm run build
, you will recieve this error:Current vs. Expected behavior
It should just bundle the
@electric-sql/pglite
without problemsProvide environment information
Which area(s) are affected? (Select all that apply)
Module Resolution, Webpack
Which stage(s) are affected? (Select all that apply)
next build (local)
Additional context
I used next
14.2.3
, the lib@electric-sql/pglite
only exportsesm
and is already minified.