@@ -9,6 +9,32 @@ const nodeRepl = require('repl');
99const kReplitPrybarInit = Symbol.for('replit.prybar.init');
1010const kReplitPrybarEvalCode = Symbol.for('replit.prybar.eval.code');
1111const kReplitPrybarEvalFile = Symbol.for('replit.prybar.eval.file');
12+let moduleMode = null;
13+const assets_dir =
14+ process.env.PRYBAR_ASSETS_DIR || path.join(process.cwd(), 'prybar_assets');
15+
16+const originalFindPath = Module._findPath;
17+Module._findPath = function(request, paths, isMain) {
18+ if (moduleMode === "cjs" && request === 'node-fetch') {
19+ // Because node-fetch version >= 3 doesn't support commonjs, we'll switch to
20+ // @replit/node-fetch if it comes to that
21+ const result = originalFindPath(request, paths, isMain);
22+ const dir = path.dirname(result);
23+ const packageJson = require(path.join(dir, "..", "package.json"));
24+ const [major] = packageJson.version.split(".").map(Number);
25+ if (major >= 3) {
26+ // Switch to @replit/node-fetch
27+ console.log("\u001b[33mAuto-switching node-fetch to @replit/node-fetch for cjs support\u001b[0m");
28+
29+ return path.join(assets_dir, "nodejs/node_modules/@replit/node-fetch/index.js");
30+ }
31+
32+ return result;
33+ } else {
34+
35+ return originalFindPath(request, paths, isMain);
36+ }
37+}
1238
1339/**
1440 * The footer we append at the end of files to run the code we want in the scope of the module its appended to.
@@ -194,9 +220,11 @@ function runModule(moduleName, isInteractive) {
194220 }
195221
196222 try {
223+ moduleMode = "cjs";
197224 require(absPath);
198225 } catch (e) {
199226 if (e.code === 'ERR_REQUIRE_ESM') {
227+ moduleMode = "esm";
200228 import(absPath);
201229 } else {
202230 throw e;