Error: secretOrPrivateKey must have a value in Node.js [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Error: secretOrPrivateKey must have a value in Node.js [Fix]

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.

index.js
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);

error secret or private key must have a value

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.

You may also get the error when trying to connect to a database, such as MySQL, MongoDB, Postgres, etc.

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.

  1. Open your terminal in your project's root directory (where your package.json file is) and run the following commands.
shell
# ๐Ÿ‘‡๏ธ If you need to initialize a package.json npm init -y # ๐Ÿ‘‡๏ธ install using NPM npm install dotenv # ๐Ÿ‘‡๏ธ or install using YARN yarn add dotenv
  1. Create a .env file in the root directory of your project and set your environment variables.
.env
SECRET_KEY="abc123"

create dot env file

Note that the .env file has to be in the root directory of your project (where your package.json file is).

  1. In the entry point of your application (e.g. index.js or app.js), import and load the dotenv package.
index.js
// ๐Ÿ‘‡๏ธ 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.

index.js
// 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.

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.

The import statement should run before any other import statements that load modules that try to access environment variables.

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:

index.js
process.env.SERCRET_KEY

With this:

index.js
`${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.

# Make sure you've added all your environment variables to your .env file

The 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.

.env
NODE_ENV=development FIRST_NAME=bobby LAST_NAME=hadz SECRET_KEY="abc123"

You can access the environment variables like so:

index.js
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);

access environment variables correctly

Here is the equivalent code sample if you use CommonJS.

index.js
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.

If the values are 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.

.env
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.

index.js
// ๐Ÿ‘‡๏ธ 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.

index.js
// ๐Ÿ‘‡๏ธ using CommonJS syntax require('dotenv').config(); console.log(process.env.SECRET_KEY);

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev