From a679f91b7359bb4b1b8777ac85c6c5f5b34b205e Mon Sep 17 00:00:00 2001 From: Pinta365 Date: Mon, 4 Mar 2024 21:01:50 +0100 Subject: [PATCH] node compatibility fix --- jsr.jsonc | 2 +- lib/filehandler.ts | 8 ++------ mod.ts | 11 +++++++++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/jsr.jsonc b/jsr.jsonc index 338c715..af5d545 100644 --- a/jsr.jsonc +++ b/jsr.jsonc @@ -1,5 +1,5 @@ { "name": "@cross/env", - "version": "0.1.0", + "version": "0.1.1", "exports": "./mod.ts" } diff --git a/lib/filehandler.ts b/lib/filehandler.ts index beb5409..462e2a8 100644 --- a/lib/filehandler.ts +++ b/lib/filehandler.ts @@ -56,12 +56,8 @@ export async function loadEnvFile( fileContent = await Bun.file(filePath).text(); break; case Runtimes.Node: { - if (typeof fs === "undefined") { - const fs = require("fs"); - fileContent = fs.readFileSync(filePath, "utf-8"); - } else { - throw new Error("Node.js 'fs' module is not available in this environment."); - } + const fs = await import("node:fs"); + fileContent = fs.readFileSync(filePath, "utf-8"); break; } default: diff --git a/mod.ts b/mod.ts index e8a9d24..db587df 100644 --- a/mod.ts +++ b/mod.ts @@ -32,7 +32,11 @@ declare const Bun: { env: Record }; // deno-lint-ignore no-explicit-any declare const require: (module: string) => any; //shims Node.js process object -declare const process: { env: Record }; +declare const process: { + // deno-lint-ignore no-explicit-any + versions: any; + env: Record; +}; // Flags to control behavior (initialized with defaults) let throwErrors = false; @@ -43,7 +47,10 @@ function getCurrentRuntime(): Runtimes { return Runtimes.Deno; } else if (typeof Bun === "object") { return Runtimes.Bun; - } else if (typeof process === "object" && typeof require === "function") { + } else if ( + typeof process === "object" && typeof process.versions !== "undefined" && + typeof process.versions.node !== "undefined" + ) { return Runtimes.Node; } else { return Runtimes.Unsupported;