DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Emmanuel Aiyenigba
Emmanuel Aiyenigba

Posted on • Updated on

How to solve "secretOrPrivateKey must have a value" in Node.js

Image description

I was writing an awesome stuff with Express today and I ran into an error, after solving it (thanks to some wonderful geeks on Stackoverflow) I thought it best to write about it just incase anyone runs into this same error in future this article will kind them on how to solve it.

what is the error and why did it occur?

I installed the dotenv dependency with the aim of storing my json web token secret inside it so as to avoid pushing a secret pass to a Github public repo.

Initially, I had put the connection pass of my MongoDB database in it with the name DB_DETAILS and I wanted to add my token secret in the next line. Well, I did just that and boooom! the terminal threw me an error warning at run time.

UnhandledPromiseRejectionWarning: Error: secretOrPrivateKey must have a value
at Object.module.exports as sign

How did I solve this?

You'd be amazed! I only add to replace process.env.TOKEN_SECRET to ${process.env.TOKEN_SECRET} using ES6 String Literals.

Yes, that worked for me. Go ahead and try it out.

explicitly configure dotenv in NestJS to solve error

Ooooh, oh! Since NestJS uses dotenv under the hood, you could easily use ConfigService or configure dotenv by changing some files.

jwt.strategy.ts

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

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      secretOrKey: process.env.TOKEN_SECRET,
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

auth.module.ts


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

@Module({
  imports: [
    JwtModule.register({
      secret: process.env.JWT_SECRET,
    }),
  ],
})
Enter fullscreen mode Exit fullscreen mode

Let me know in the comments if it works for you!

Oldest comments (7)

Collapse
 
brunahirano profile image
brunahirano

Thank you, it helped me a lot!!

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba Author

Wow! I'm glad to hear this.

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba Author

Btw, I just followed you on GitHub @brunahirano . Nice repos you have on there!

Collapse
 
lucianogmoraesjr profile image
Luciano Moraes Jr.

Yea man! This saved me. Thank you

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba Author

You're welcome. I'm happy you find it useful.

Collapse
 
nahid570 profile image
Nahid Faraji

WOW, I can't thank you enough

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba Author

You're welcome.

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›