Last updated: Apr 5, 2024
Reading timeยท4 min
The Node.js error "secretOrPrivateKey must have a value" occurs when your application isn't able to read your environment variables correctly.
To solve the error, install and use the dotenv
package to load your
environment variables.
Here is a simple example of how the error occurs.
import jwt from 'jsonwebtoken'; // ๐๏ธ If you use CommonJS // const jwt = require('jsonwebtoken') // โ๏ธ Error: secretOrPrivateKey must have a value const token = jwt.sign('bobbyhadz.com', process.env.SECRET_KEY);
We are trying to read the value of the SECRET_KEY
environment variable but we
haven't enabled our application to do so.
The code sample above is quite simple.
However, the cause of the error is the same - your application isn't able to read the values of the environment variables you're using.
The process.env.SECRET_KEY
property has a value of undefined
in the example.
The easiest way to load environment variables in your Node.js application is to use the dotenv package.
package.json
file is) and run the following commands.# ๐๏ธ If you need to initialize a package.json npm init -y # ๐๏ธ install using NPM npm install dotenv # ๐๏ธ or install using YARN yarn add dotenv
.env
file in the root directory of your project and set your
environment variables.SECRET_KEY="abc123"
Note that the .env
file has to be in the root directory of your project (where
your package.json
file is).
index.js
or app.js
), import
and load the dotenv
package.// ๐๏ธ Using ES Modules import/export syntax import 'dotenv/config'; import jwt from 'jsonwebtoken'; const token = jwt.sign('bobbyhadz.com', process.env.SECRET_KEY); console.log(token);
The example above uses the ES6 Modules import/export syntax.
If you use the CommonJS syntax, use the following code sample instead.
// Using CommonJS syntax require('dotenv').config(); const jwt = require('jsonwebtoken'); const token = jwt.sign('bobbyhadz.com', process.env.SECRET_KEY); console.log(token);
If I run the file using node
, I can see that the error is resolved.
Note that the import that loads the dotenv
module should be placed in the
entry point of your application, as early as possible.
If the dotenv
import statement hasn't run and you try to access an
environment variable, its value will be undefined
.
If the issue persists, try to use a template literal string when accessing the environment variable.
Replace this:
process.env.SERCRET_KEY
With this:
`${process.env.SECRET_KEY}`
Notice that we used backticks `, and not single quotes when wrapping the template string.
The dollar sign and curly braces syntax enables you to interpolate a variable in the string.
.env
fileThe error is also caused if you forget to add the environment variable you are
trying to access to your .env
file or misspell it.
If your .env
file looks as follows.
NODE_ENV=development FIRST_NAME=bobby LAST_NAME=hadz SECRET_KEY="abc123"
You can access the environment variables like so:
import 'dotenv/config'; console.log(process.env.NODE_ENV); console.log(process.env.FIRST_NAME); console.log(process.env.LAST_NAME); console.log(process.env.SECRET_KEY);
Here is the equivalent code sample if you use CommonJS.
require('dotenv').config(); console.log(process.env.NODE_ENV); console.log(process.env.FIRST_NAME); console.log(process.env.LAST_NAME); console.log(process.env.SECRET_KEY);
Make sure you haven't misspelled the environment variables.
If you misspell an environment variable, its value will be undefined
which
causes the error.
If the error persists, try to console.log()
the values of your environment
variables.
undefined
, make sure you don't have any spaces or special characters in the keys of the environment variables.Another thing you can try is to wrap the values of the environment variables in double quotes.
SECRET_KEY="abc123"
If the values of your environment variables contain spaces or special characters, they have to be double-quoted.
You also have to double-quote environment variables that span multiple lines.
If the error persists, try to load the dotenv
package in the file in which you
are accessing the environment variables.
Here is an example that uses the ES6 Modules import/export syntax.
// ๐๏ธ using ES Modules import/export syntax import 'dotenv/config'; console.log(process.env.SECRET_KEY);
If you use the CommonJS syntax, use the following code sample instead.
// ๐๏ธ using CommonJS syntax require('dotenv').config(); console.log(process.env.SECRET_KEY);
You can learn more about the related topics by checking out the following tutorials: