Open rosszurowski opened 11 months ago
I am also facing the same issue. In my case I am not able to run the nextjs dev server. Here are the logs:
- wait compiling /page (client and server)...
- error ./node_modules/@libsql/client/LICENSE
Module parse failed: Unexpected token (1:4)
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
> MIT License
|
| Copyright (c) 2023 libSQL
Import trace for requested module:
./node_modules/@libsql/client/LICENSE
./node_modules/@libsql/ sync ^\.\/.*$
./node_modules/libsql/index.js
./node_modules/@libsql/client/lib-esm/sqlite3.js
./node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
./src/app/page.tsx
- wait compiling...
- warn ./node_modules/@neon-rs/load/dist/index.js
Critical dependency: the request of a dependency is an expression
Import trace for requested module:
./node_modules/@neon-rs/load/dist/index.js
./node_modules/libsql/index.js
./node_modules/@libsql/client/lib-esm/sqlite3.js
./node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
./src/app/page.tsx
./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/Users/zainzafar/workplace/talking-sites/node_modules/node-fetch/lib'
Import trace for requested module:
./node_modules/node-fetch/lib/index.js
./node_modules/cross-fetch/dist/node-ponyfill.js
./node_modules/cross-fetch/dist/node-polyfill.js
./node_modules/@pinecone-database/pinecone/dist/v0/index.js
./node_modules/@pinecone-database/pinecone/dist/index.js
./src/lib/db/pinecone.ts
./src/app/page.tsx
./node_modules/ws/lib/buffer-util.js
Module not found: Can't resolve 'bufferutil' in '/Users/zainzafar/workplace/talking-sites/node_modules/ws/lib'
Import trace for requested module:
./node_modules/ws/lib/buffer-util.js
./node_modules/ws/lib/websocket.js
./node_modules/ws/wrapper.mjs
./node_modules/@libsql/isomorphic-ws/node.mjs
./node_modules/@libsql/hrana-client/lib-esm/index.js
./node_modules/@libsql/client/lib-esm/http.js
./node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
./src/app/page.tsx
./node_modules/ws/lib/validation.js
Module not found: Can't resolve 'utf-8-validate' in '/Users/zainzafar/workplace/talking-sites/node_modules/ws/lib'
Import trace for requested module:
./node_modules/ws/lib/validation.js
./node_modules/ws/lib/sender.js
./node_modules/ws/wrapper.mjs
./node_modules/@libsql/isomorphic-ws/node.mjs
./node_modules/@libsql/hrana-client/lib-esm/index.js
./node_modules/@libsql/client/lib-esm/http.js
./node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
./src/app/page.tsx
Same issue here.
@rosszurowski @zaingz @K1NXZ can you please test 0.3.5-pre.2 (that switched from better-sqlite3 to libsql) with the following additional configuration:
https://github.com/libsql/libsql-client-ts#using-the-library-with-nextjs
The extra config is not needed if you use a version of Next with https://github.com/vercel/next.js/pull/55266 included.
@penberg
@rosszurowski @zaingz @K1NXZ can you please test 0.3.5-pre.2 (that switched from better-sqlite3 to libsql) with the following additional configuration:
https://github.com/libsql/libsql-client-ts#using-the-library-with-nextjs
The extra config is not needed if you use a version of Next with vercel/next.js#55266 included.
I just tested it with 0.3.5-pre.2
on nextjs version 13.4.20-canary.28
& 13.4.19
and get following compilation error now:
Failed to compile
./node_modules/.pnpm/@libsql+client@0.3.5-pre.2/node_modules/@libsql/client/LICENSE
Module parse failed: Unexpected token (1:4)
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
> MIT License
|
| Copyright (c) 2023 libSQL
Also running it with or without the experimental flag in the next config serverComponentsExternalPackages: ["libsql"]
did not change anything.
On @libsql/client: 0.3.4
and next: 13.4.19
, with or without the Nextjs experimental flags, I get errors from not being able to find the following modules, but can still connect and use the database:
encoding
, bufferutil
, utf-8-validate
.
Here's an example stack trace of the errors:
Module not found: Can't resolve 'encoding' in 'C:\...\{workspace}\node_modules\.pnpm\node-fetch@2.7.0\node_modules\node-fetch\lib'
Import trace for requested module:
./node_modules/.pnpm/node-fetch@2.7.0/node_modules/node-fetch/lib/index.js
./node_modules/.pnpm/@libsql+isomorphic-fetch@0.1.10/node_modules/@libsql/isomorphic-fetch/node.js
./node_modules/.pnpm/@libsql+hrana-client@0.5.5/node_modules/@libsql/hrana-client/lib-esm/index.js
./node_modules/.pnpm/@libsql+client@0.3.4/node_modules/@libsql/client/lib-esm/http.js
./node_modules/.pnpm/@libsql+client@0.3.4/node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
When upgrading to @libsql/client: 0.3.5-pre.9
, with or without Nextjs experimental flags, I get a more sever error and my app is unusable:
Module parse failed: Unexpected token (1:4)
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
> MIT License
|
| Copyright (c) 2023 libSQL
Import trace for requested module:
./node_modules/.pnpm/@libsql+client@0.3.5-pre.9/node_modules/@libsql/client/LICENSE
./node_modules/.pnpm/node_modules/@libsql/ ./node_modules/@libsql/ ./node_modules/.pnpm/node_modules/@libsql/ sync ^\.\/.*$
./node_modules/.pnpm/libsql@0.1.14/node_modules/libsql/index.js
./node_modules/.pnpm/@libsql+client@0.3.5-pre.9/node_modules/@libsql/client/lib-esm/sqlite3.js
./node_modules/.pnpm/@libsql+client@0.3.5-pre.9/node_modules/@libsql/client/lib-esm/node.js
./src/lib/db/index.ts
Hope this helps.
Any solutions to this yet??
Unfortunately serverComponentsExternalPackages: ["libsql"],
doesn't fix the issue.
After extensive testing, I noticed that this error occurs when using pnpm. No issues when using yarn or npm. Quick fix: use yarn or npm.
If you're using a Turbo repo (monorepo) like me, add the following to your .npmrc
:
public-hoist-pattern[]=*libsql*
Not entirely sure this is the correct solution, but at least it's working for me now.
As @linyiru has mentioned above, the error Failed to compile
occurs when using pnpm
. I can add that it specifically occurs when pnpm
is used without --shamefully-hoist
.
Here is a minimal reproduction repo: my test repo.
The ReadMe contains information on how to compare the effects of npm i
, pnpm i
, and pnpm i --shamefully-hoist
.
Here is a link to a Codesandbox for the same repo. Initially it should run npm i
and successfully dev env, but I encourage you to clear node_modules by running rm -rf node_modules
and first pnpm i
to see the error, then clear node_modules again and run pnpm i --shamefully-hoist
to see that the dev env starts up successfully.
I will probably open a new issue regarding this error.
As for dealing with the main purpose of this issue thread: the warnings. I recommend taking a look at what this repo is doing to solve these errors, spoiler: it involves editing the webpack config in your next.config.ts
. (Source line). This stopped the warning messages for me.
webpack: (config) => {
// config.externals is needed to resolve the following errors:
// Module not found: Can't resolve 'bufferutil'
// Module not found: Can't resolve 'utf-8-validate'
config.externals.push({
bufferutil: 'bufferutil',
'utf-8-validate': 'utf-8-validate',
});
return config;
},
Adding the following stopped the warning messages. Using "@libsql/client": "^0.3.5"
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ["@libsql/client"]
}
}
It looks like this will be automatically handled for you soon - https://github.com/vercel/next.js/pull/56192
Adding the following stopped the warning messages. Using
"@libsql/client": "^0.3.5"
const nextConfig = { experimental: { serverComponentsExternalPackages: ["@libsql/client"] } }
It looks like this will be automatically handled for you soon - vercel/next.js#56192
this is still giving me errors when used directly in a server component
UPDATE: apparently this does resolves the issue, but if you somehow call libsql (or drizzle()) in a server component but incorrectly marked with use client
, you will still get this module errors instead of the usual error that is along the lines of cannot use server components in files marked with use client
. Granted this is not something you'll ever do but it took a lot of my time looking at my next.config files when there's just one line of use client
that i forgot to delete when i'm migrating a client component into a server one.
TL;DR: If anyone still gets the error after the above solutions, double check your files and make sure components calling libsql are not marked with use client
since you won't get the normal errors if you do
When using
@libsql/client
with Next.js (I've only tested in the app router), the client spits out a number of warnings about resolving modules.Here's a small sample:
You can see them in this reproduction sandbox. I've also copied the full set of warnings I'm seeing on a Vercel build below:
Show warnings from Vercel build output
It seems like these warnings all stem from the
ws
,node-fetch
, andbetter-sqlite3
packages. I suspect it has something to do with how Webpack (which Next.js uses under the hood) resolves these modules, but I've never seen these errors for other packages.One recommendation I might suggest for the
node-fetch
issues is consider using the new built-infetch
which is available since Node 18, or use undici, which is an official Node.js project, and I believe can be used to bring that fetch API to older versions.