I'm using dotenv to declare JWT_SECRET env variable and It's showing the error mentioned in title.
.env
NODE_ENV="development"
JWT_SECRET="mySecretString"
environment.d.ts
import { Secret } from 'jwt-promisify'
declare global {
namespace NodeJS {
interface ProcessEnv {
JWT_SECRET: Secret,
NODE_ENV: 'development' | 'production',
PORT?: number | string
}
}
}
export {}
I'm using in my routes file im signing token with JWT_SECRET
route.ts
const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET)
Here intellisense is working but when I run the app or compile it the error appears.
error
error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'.
Type 'undefined' is not assignable to type 'Secret'.
32 const token = await jwt.sign({ id: newUser.id }, process.env.JWT_SECRET)
~~~~~~~~~~~~~~~~~~~~~~
import dotenv from 'dotenv' dotenv.config()
in the top of file?process.env
app.js
only then it worked with other files as well