Skip to content

Using with Vite: Uncaught ReferenceError: require is not defined #8738

@raymondboswel

Description

@raymondboswel

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

I'm using TypeORM in a Vite based project, and when building the project in production mode, I get the following error

Uncaught ReferenceError: require is not defined

which is thrown from BrowserPlatformTools:150

I understand that this is not necessarily a common use case, and I'm not sure if this change would break other clients, so if that's the case feel free to close my issue, and I'll just carry on using the patch-package workaround.

Here is the diff that solved my problem:

diff --git a/node_modules/typeorm/browser/platform/BrowserPlatformTools.js b/node_modules/typeorm/browser/platform/BrowserPlatformTools.js
index 7b23ac5..123b61d 100644
--- a/node_modules/typeorm/browser/platform/BrowserPlatformTools.js
+++ b/node_modules/typeorm/browser/platform/BrowserPlatformTools.js
@@ -5,6 +5,8 @@
  * For node.js environment this class is not getting packaged.
  * Don't use methods of this class in the code, use PlatformTools methods instead.
  */
+import Buffer from "buffer";
+
 var PlatformTools = /** @class */ (function () {
     function PlatformTools() {
     }
@@ -147,7 +149,7 @@ var Writable = /** @class */ (function () {
 }());
 export { Writable };
 if (typeof window !== "undefined") {
-    window.Buffer = require("buffer/").Buffer;
+    window.Buffer = Buffer;
 }
 // NativeScript uses global, not window
 if (typeof global !== "undefined") {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions