2

I am using Next.js 12.1.5 to build a website, the client wants the form responses on the website to be sent directly to her email, so I am using a library EmailJS. EmailJS has a send method to send the form data, and you have to enter 3 string parameters to that method which are secret keys. This code is running on the client side so I have prefixed all my .env variables with NEXT_PUBLIC. It is loading the variables fine, I am just using typescript and get a type error that the .env variables are of type string | undefined and I need to pass a string. Is there any workaround other than just making the file a .js file?

I have followed multiple questions on here and still don't have a solution.

  • 1
    You can assert that it isn't `undefined` with `!` after it: `process.env.NEXT_PUBLIC_MY_ENV_VAR!`. – kelsny Jun 01 '22 at 18:22
  • Thank you this worked to remove the error, I'm trying to right everything in Typescript now and am still learning. – Ryan Speciale Jun 02 '22 at 12:50

0 Answers0