.env Issue
Answered
Blue Picardy Spaniel posted this in #help-forum
Blue Picardy SpanielOP
I hae the following code in a TS file:
However I'm getting this error:
export const authConfig: NextAuthOptions = {
providers: [
DiscordProvider({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
authorization: "https://discord.com/api/oauth2/authorize?scope=identify"
})
]
};However I'm getting this error:
Type 'undefined' is not assignable to type 'string' even though I created a typings.d.ts file with the following inside:declare global {
namespace NodeJS {
interface ProcessEnv {
CLIENT_ID: string;
CLIENT_SECRET: string;
}
}
}Answered by joulev
i suggest you use https://env.t3.gg for this. not only you get type-safe
env, you also get build time validation17 Replies
@Blue Picardy Spaniel I hae the following code in a TS file:
ts
export const authConfig: NextAuthOptions = {
providers: [
DiscordProvider({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
authorization: "https://discord.com/api/oauth2/authorize?scope=identify"
})
]
};
However I'm getting this error: `Type 'undefined' is not assignable to type 'string'` even though I created a `typings.d.ts` file with the following inside:
ts
declare global {
namespace NodeJS {
interface ProcessEnv {
CLIENT_ID: string;
CLIENT_SECRET: string;
}
}
}
i suggest you use https://env.t3.gg for this. not only you get type-safe
env, you also get build time validationAnswer
@Blue Picardy Spaniel I hae the following code in a TS file:
ts
export const authConfig: NextAuthOptions = {
providers: [
DiscordProvider({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
authorization: "https://discord.com/api/oauth2/authorize?scope=identify"
})
]
};
However I'm getting this error: `Type 'undefined' is not assignable to type 'string'` even though I created a `typings.d.ts` file with the following inside:
ts
declare global {
namespace NodeJS {
interface ProcessEnv {
CLIENT_ID: string;
CLIENT_SECRET: string;
}
}
}
Carolina Dog
Just use as string or you can also add + "" in the clientId and clientSecret
@joulev i suggest you use https://env.t3.gg for this. not only you get type-safe `env`, you also get build time validation
Blue Picardy SpanielOP
thanks i’ll have a look
@Carolina Dog Just use as string or you can also add + "" in the clientId and clientSecret
Blue Picardy SpanielOP
no i want autocompletion as well
@joulev i suggest you use https://env.t3.gg for this. not only you get type-safe `env`, you also get build time validation
Blue Picardy SpanielOP
Works amazingly, do I still need dotenv?
@Blue Picardy Spaniel Works amazingly, do I still need dotenv?
Giant panda
You never needed it in the first place.
@Carolina Dog Just use as string or you can also add + "" in the clientId and clientSecret
Giant panda
That is an absolutely terrible suggestion as you end up with a possibly undefined object concatenated to an empty string.
It doesn't relate at all to the missing types.
But using the t3-env package is a very convenient way to work with var that I also highly recommend.
@Giant panda You never needed it in the first place.
Blue Picardy SpanielOP
what? why?
Giant panda
Because env vars are already part of
process.env and sourced by Next itself@Giant panda Because env vars are already part of `process.env` and sourced by Next itself
Blue Picardy SpanielOP
ah ok
unrelated but if i use t3-env with another project do i need dotenv too?
Giant panda
If you use it with Vite, you don't need it either because Vite will source the variables as well.
@Giant panda If you use it with Vite, you don't need it either because Vite will source the variables as well.
Blue Picardy SpanielOP
no just a bare node project
@Blue Picardy Spaniel no just a bare node project
I do think that if you need dotenv without t3-env you will still need dotenv with t3-env
It just does the validation of process.env, it doesn’t load stuff into process.env