question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"require is not defined" when initializing DB with d1 beta bindings

See original GitHub issue

Was trying out the d1 beta bindings in miniflare, and ran into the following:

[mf:err] ReferenceError: require is not defined
    at _resolve (file:///Users/tgriesser/Github/cf-test/node_modules/.pnpm/npx-import@1.1.3/node_modules/npx-import/lib/utils.js:11:5)
    at npxResolve (file:///Users/tgriesser/Github/cf-test/node_modules/.pnpm/npx-import@1.1.3/node_modules/npx-import/lib/index.js:56:16)
    at createSQLiteDB (/Users/tgriesser/Github/cf-test/node_modules/.pnpm/@miniflare+shared@2.9.0/node_modules/@miniflare/shared/src/sqlite.ts:20:51)
    at FileStorage.getSqliteDatabase (/Users/tgriesser/Github/cf-test/node_modules/.pnpm/@miniflare+storage-file@2.9.0/node_modules/@miniflare/storage-file/src/index.ts:114:21)

Seems it’s due to this code in npx-import, which is a package of "type": "module":

export function _resolve(packageWithPath: string) {
  return require.resolve(packageWithPath)
}

I believe this needs to be changed to create require:

export function _resolve(packageWithPath: string) {
  return createRequire(import.meta.url).resolve(packageWithPath)
}

Tried to patch and write a test for it in the npx-import package, but couldn’t figure out getting vitest to load the test in a way that it’d fail, where require is undefined. Didn’t see a way to force load as a true esm environment, seems require is always defined in the worker context.

The other fix that seemed to work was to change:

https://github.com/cloudflare/miniflare/blob/974c7c2796cfa243f8eb0569cd2f37b59795ed71/packages/shared/src/sqlite.ts#L19-L21

to just use require.resolve directly:

return new DatabaseConstructor(dbPath, {
  nativeBinding: getSQLiteNativeBindingLocation(require.resolve("better-sqlite3")),
});

Figured @geelen would know better where is best to fix this, or if there was something else I was missing here where the current code is expected.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Skye-31commented, Nov 19, 2022

This should be shipped on any fresh installation of Miniflare/Wrangler - this issue can be closed 🙂

0reactions
BeeeQueuecommented, Nov 19, 2022

I just installed/updated them when I wrote that comment and had to add the resolution. 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

require is not defined? Node.js - javascript - Stack Overflow
It happens when you declare your package type as module in your package.json . If you do this, certain CommonJS variables can't be...
Read more >
ReferenceError: require is not defined #33741 - nodejs/node
console.log(require); Run //node-v12.18.0-linux-x64/bin/node test.js; Node returns error output stating that require is not defined.
Read more >
MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
If binding to the address fails, the server produces an error and does not start. The admin_address system variable is similar to the...
Read more >
Dependency injection with Hilt | Android Developers
Each Hilt component comes with a set of default bindings that Hilt can inject as dependencies into your own custom bindings. Note that...
Read more >
XSL Transformations (XSLT) Version 3.0 - W3C
Abstract. This specification defines the syntax and semantics of XSLT 3.0, a language designed primarily for transforming XML documents into ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found