0

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/dotenv@v3.2.2/mod.ts59) at config (https://deno.land/x/dotenv@v3.2.2/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

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • 1
    In short: use `dotenv` from https://deno.land/std/dotenv — it was moved from `/x` (third-party) to `/std` (first party). For details, see [denoland/deno_std#1957](https://github.com/denoland/deno_std/issues/1957). Does that address your question? If so, I can write it up as an answer — if not, what am I missing? – jsejcksn May 16 '23 at 01:57
  • I have switiched to using automatic loads with ` import "https://deno.land/std@0.173.0/dotenv/load.ts"; `, everything works fine in my local but deno deploy just can't read the env vars I provided. I have updated the screenshots in teh question – Mohamed Abdinasir May 16 '23 at 03:16
  • 2
    The details in your [recent edit](https://stackoverflow.com/revisions/76258837/2) completely _change the meaning_ of the [original](https://stackoverflow.com/revisions/76258837/1) question, which is [not appropriate for an edit](https://stackoverflow.com/help/editing). I [reverted](https://stackoverflow.com/revisions/76258837/3) it back to the original — if you have modified your initial code since asking the question in a way that now creates a new issue, you can consider [asking a new question](https://stackoverflow.com/questions/ask), but be sure to first review [ask]. – jsejcksn May 16 '23 at 21:29

1 Answers1

0

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/dotenv@v3.2.0/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/std@0.188.0/dotenv/mod.ts";
console.log(await load());
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • This solution does seem to work because I don't see those log errors anymore, however I am unable to view my live deployment anymore. It says page can’t be found. – Mohamed Abdinasir May 19 '23 at 06:25
  • As someone else pointed out this is actually in the std library now too so I would use the std version of this now as well. But if you have other errors then I think that is unrelated to this question. Can you mark this as the answer for now? – justin.m.chase May 19 '23 at 13:17
  • I guess not because I can't load the local env vars now. I forgot to mention that. – Mohamed Abdinasir May 19 '23 at 20:08