Last modified: Jun 22, 2026

Fix jsonwebtoken Module Error

Getting the error "Cannot find module 'jsonwebtoken'" in Node.js is common. It stops your app from running. This guide shows you simple fixes.

The error means Node.js cannot locate the jsonwebtoken package. This package handles JSON Web Tokens (JWT). You need it for authentication in many apps.

Let's fix this step by step. We will cover installation, paths, and cache issues.

What Causes This Error?

Several things cause this error. The most common reason is a missing node_modules folder. Another reason is a wrong import path. A corrupted npm cache also triggers it.

Beginners often forget to install the package. Others run the script from the wrong directory. Let's check each case.

Step 1: Install jsonwebtoken

First, ensure the package is installed. Open your terminal in your project folder. Run this command:


npm install jsonwebtoken

This adds jsonwebtoken to your node_modules folder. It also updates your package.json file. If you use yarn, run:


yarn add jsonwebtoken

If you need a specific version, add the version number. For example:


npm install [email protected]

After installation, try running your app again.

Step 2: Check Your Import Statement

Make sure you import the module correctly. In modern JavaScript (ES modules), use import:


import jwt from 'jsonwebtoken';

In CommonJS (older style), use require:


const jwt = require('jsonwebtoken');

Do not add a path like ./jsonwebtoken. The package is in node_modules, not your local folder. A wrong path causes the error.

Step 3: Verify Your Working Directory

Run your script from the correct folder. The node_modules folder must be in the same directory as your script. If you are one level up, Node.js cannot find it.

Check your current folder with pwd on Mac/Linux or cd on Windows. Then navigate to your project root:


cd /path/to/your/project
node app.js

If your script is in a subfolder, node_modules must still be in the root. Node.js searches parent folders, but it's safer to run from the root.

Step 4: Delete and Reinstall node_modules

A corrupted node_modules folder causes many errors. Delete the folder and reinstall all packages:


# Delete node_modules and package-lock.json
rm -rf node_modules package-lock.json

# Reinstall all packages
npm install

This ensures a clean install. It fixes missing or broken files. If you use yarn, delete yarn.lock instead.

Step 5: Clear npm Cache

A corrupted npm cache can also cause this error. Clear the cache with:


npm cache clean --force

Then reinstall the package again. This removes any bad cached data.

Step 6: Check package.json

Look at your package.json file. Make sure jsonwebtoken is listed under dependencies. If it is missing, add it manually:


{
  "dependencies": {
    "jsonwebtoken": "^9.0.0"
  }
}

Then run npm install. This installs the missing package.

Step 7: Use a Version Manager

Sometimes Node.js version causes issues. Use nvm (Node Version Manager) to switch versions. Install the latest LTS version:


nvm install --lts
nvm use --lts

Then reinstall your packages. This solves compatibility problems.

Example Code That Works

Here is a full example. It creates a simple JWT token:


// Import jsonwebtoken
const jwt = require('jsonwebtoken');

// Create a payload
const payload = { userId: 123, role: 'admin' };

// Secret key (keep this secret in production)
const secret = 'mySuperSecretKey123';

// Generate a token
const token = jwt.sign(payload, secret, { expiresIn: '1h' });

// Print the token
console.log('Token:', token);

// Verify the token
jwt.verify(token, secret, (err, decoded) => {
  if (err) {
    console.log('Verification failed:', err.message);
  } else {
    console.log('Decoded payload:', decoded);
  }
});

Output:


Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEyMywicm9sZSI6ImFkbWluIiwiaWF0IjoxNzA5OTk5OTk5LCJleHAiOjE3MTAwMDM1OTl9.abc123
Decoded payload: { userId: 123, role: 'admin', iat: 1709999999, exp: 1710003599 }

This code runs without the error if the package is installed correctly.

Common Mistakes to Avoid

Do not install the package globally. Use npm install jsonwebtoken without the -g flag. Global packages are not available to local scripts.

Do not rename the node_modules folder. Keep it as is. Node.js only looks for that exact name.

Do not use a relative path like require('./jsonwebtoken'). This looks for a local file, not the npm package.

When All Else Fails

If the error persists, try creating a new project. Initialize a fresh package.json with npm init -y. Then install jsonwebtoken again. This isolates the problem.

Check your Node.js version with node -v. Older versions may not support some features. Upgrade to at least Node.js 16.

If you still face issues, read our detailed guide on Fix Node.js Error: Cannot find module. It covers general fixes for any missing module error.

Conclusion

The "Cannot find module 'jsonwebtoken'" error is easy to fix. Start by installing the package with npm install jsonwebtoken. Then check your import statement and working directory.

If the problem continues, delete node_modules and reinstall. Clear the npm cache as a last resort. Always run your script from the project root folder.

With these steps, your app will work with JWT authentication. Test your code after each fix. This saves time and prevents frustration.