jsonwebtoken
jsonwebtoken is the most widely used library for creating and verifying JSON Web Tokens (JWTs) in Node.js. JWTs are compact, URL-safe tokens that encode claims …
Installation
npm install jsonwebtoken
yarn add jsonwebtoken
pnpm add jsonwebtoken
Import
import jwt from 'jsonwebtoken';
Quick Example
import jwt from 'jsonwebtoken';
const token = jwt.sign(
{ userId: 123, role: 'admin' },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);
const decoded = jwt.verify(token, process.env.JWT_SECRET);
console.log(decoded.userId); // 123About jsonwebtoken
jsonwebtoken is the most widely used library for creating and verifying JSON Web Tokens (JWTs) in Node.js. JWTs are compact, URL-safe tokens that encode claims (data) as a JSON payload, signed with a secret (HMAC) or public/private key pair (RSA, ECDSA) to ensure integrity and authenticity. The library provides jwt.sign() for creating tokens with payload data, expiration times, issuers, and audiences, and jwt.verify() for validating tokens and extracting the decoded payload. JWTs are commonly used for stateless authentication in REST APIs — after login, the server issues a JWT that the client includes in subsequent requests, allowing the server to verify identity without session storage. The library supports symmetric signing (HS256, HS384, HS512) using a shared secret, and asymmetric signing (RS256, RS384, RS512, ES256, ES384, ES512, PS256) using RSA or ECDSA key pairs. jsonwebtoken handles token expiration checking, not-before claims, issuer and audience validation, and provides options for ignoring expiration during development. While the jose library offers a more modern, standards-compliant alternative, jsonwebtoken remains the most popular JWT library due to its simplicity and widespread adoption.
Quick Facts
| Package | jsonwebtoken |
| Category | Auth |
| Weekly Downloads | 15M+ |
| License | MIT |
| Install | npm install jsonwebtoken |
Related Packages
jose is a universal, standards-compliant JavaScript library implementing JSON Object Signing and Enc…
Passport is the most popular authentication middleware for Node.js, providing a modular framework fo…
bcrypt is a library for hashing passwords using the bcrypt algorithm, which is specifically designed…
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
Helmet helps secure Express.js applications by setting various HTTP response headers that protect ag…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.