I'm using a library that requires a secret string. I've set my node variable like so:
export JWT_SECRET=e177920e88165bd0090b1c6b544cf7
However, when I try to use it in my app, like so:
const jwt = require('jsonwebtoken');
function userToken(user) {
return jwt.sign({
user: user.id,
}, process.env.JWT_SECRET);
}
it hits an error that says the secret must be a string or buffer. I thought node variables are strings, so not sure what the issue is. Thanks.
Hmm, this works fine for me. Maybe try adding a console.log(process.env.JWT_SECRET) to check that your env var is being loaded correctly.
As a side note, I would also consider using some kind of environment variable management library such as dotenv. It allows you to store a all of your environment variables in a .env file like:
JWT_SECRET="abc"
OTHER_ENV_VAR="def"
It makes it much easier to keep track of your env vars :)
Be sure to use the same terminal you exported the environment variable out of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With