Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error resolving commonjs request for [project]/node_modules/better-sqlite3/lib/database.js #967

Closed
aretrace opened this issue Mar 11, 2023 · 2 comments

Comments

@aretrace
Copy link

aretrace commented Mar 11, 2023

I am attempting to use Next.js 13 new app directory (trying out RSCs) with better-sqlite3 (through Drizzle ORM, the issue is the same with just better-sqlite3), I get the following error:

... warn - ./node_modules/better-sqlite3/lib/database.js Critical dependency: the request of a dependency is an expression ... Import trace for requested module: ./node_modules/better-sqlite3/lib/database.js ./node_modules/better-sqlite3/lib/index.js ./src/db/client.ts ./src/app/page.tsx error - Failed to generate static paths for /: TypeError: Cannot read properties of undefined (reading 'indexOf') ...

My codebase is a bare-bones skeleton:

src/
├── app/
│ ├── ...
│ ├── page.tsx
│ └── ...
└── db/
├── ...
├── client.ts
└── ...

In client.ts I export a const db from the new Database api and I import it in page.tsx where I attempt to execute a simple query (trying both outside and inside the react component function), the error stems from the instantiation of Database with nextjs displaying the following error in the browser:

Server Error TypeError: Cannot read properties of undefined (reading 'indexOf')
...
export const db = new Database('sqlite.db')
^

I am fairly confident all statements are running only on the server (I used the 'use server' directive and get the same error).

@aretrace
Copy link
Author

Got rid of the error by adding this external webpack modules configuration option in next.config.js:

webpack: (config) => {
    config.externals = {
      'better-sqlite3': 'commonjs better-sqlite3',
    }
    return config
  },

Is this practice mentioned somewhere in the docs? Are there any "gotchas" with this?

@aretrace
Copy link
Author

See @barthicus answer at drizzle-team/drizzle-orm#249, would be fixed by vercel/next.js#47327

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant