TypeError: Deno.readFileSync is not a function

401 views Asked by At

I am running into this error when I deploy my project to deno deploy.

TypeError: Deno.readFileSync is not a function at parseFile (https://deno.land/x/[email protected]/mod.ts59) at config (https://deno.land/x/[email protected]/mod.ts:41:18) at file:///src/src/controllers/users/getUsers.ts:2:19

It's complaining about Prisma db source which it can't read from the env vars although I have provided all my env vars in the project settings. Has anyone else encountered this before or able to guide me. Below are screenshots of the error, the part of code that it's complaining about and the deps file.

enter image description here enter image description here enter image description here

1

There are 1 answers

3
justin.m.chase On

You have to only import the configAsync function, the config function attempts to call the synchronous function as you noticed.

Here is an example of my code importing it for Deno Deploy:

// Deno deploy does not allow sync file reads
// You must use the async api
export { configAsync as dotenv } from "https://deno.land/x/[email protected]/mod.ts";

Here is an example of using it:

import { dotenv } from "../deps/dotenv.ts";

export async function getEnv() {
  return {
    ...await dotenv(),
    ...Deno.env.toObject(),
  };
}

But probably the more correct way will be to use the replacment dotenv from the std library.

import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
console.log(await load());