7

I am saving my jwt token secret into the .env file.

JWT_SECRET="secretsecret"

Now when I try to fetch the value using process.env.JWT_SECRET I get error as

Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'

I am trying to learn typescript but facing this issue with .env please guide me.

3 Answers 3

9

First of all, to load the environment constants (.env file) into your program you must install dotenv (npm install dotenv --save) and add the following into the .ts file:

import * as dotenv from 'dotenv'
dotenv.config()

Note: set it at the top of the .ts file.

Sign up to request clarification or add additional context in comments.

5 Comments

I would also recommend dotenv-flow, which builds on dotenv to add support for local overrides in .env.local. This is helpful in most typical shared repositories, so you check in global config in .env and allow each developer to add custom overrides in .env.local that's omitted from source control. It also adds support for .env.${NODE_ENV} for environment-specific config, which is a nice starting point for multiple deployments.
@superhawk610 exactly what I was searching for. We can't use nestjs in some of our libraries, but we still want the local and env-specific configs. Cheers!
This doesn't really answer the question ^ he's able to get the constants, but its the wrong type. The lower answer is the correct one
.env is typically omitted from source control already @superhawk610
Node v20 has --env key so u dont need any 3d paty. However dotenvx is much better in many senses, recommend to try it out
4

The error is arising because a parameter of typeSecret is required but process.env.JWT_SECRET is of type string|undefined

To resolve this issue import {Secret} from "jsonwebtoken" and add process.env.JWT_SECRET as Secret instead of just process.env.JWT_SECRET

Comments

-3

Environment variables are supported out of the box with Node and are accessible via the env object (which is a property of the process global object.)

To see this in action, you can create your own environment variable right in the Node REPL by appending a variable to the process.env object directly.

To create environment variables in your Node app, you will probably want to use a package like DotEnv.

DotEnv is a lightweight npm package that automatically loads environment variables from a .env file into the process.env object.

To use DotEnv, first install it using the command: npm i dotenv Then in your app, require and configure the package like this: require('dotenv').config()

You can declare multiple variables in the .env file. For example, you could set database-related environment variables like this:

DB_HOST=localhost
DB_USER=admin
DB_PASSWORD=password

There is no need to wrap strings in quotation marks. DotEnv does this automatically for you.

Accessing your variables is super easy! They are attached to the process.env object, so you can access them using the pattern process.env.KEY.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.