From 18a33668524c0f593e8379b67619ce45829c7f42 Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Fri, 8 Aug 2025 17:38:23 -0400 Subject: [PATCH] Update export styles for ESM compatibility Node.js's ESM-to-CJS interoperability only supports a limited subset of exports. (See [cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer/blob/main/README.md) for details.) By switching to one of the supported styles, ESM code can use named imports from jsonwebtoken. To demonstrate, create a `test.mjs`: ```js // Within the jsonwebtoken directory: import { verify } from './index.js'; // Or importing from an installed jsonwebtoken package: import { verify } from 'jsonwebtoken'; verify('a', 'a'); ``` Without this change: ``` import { verify } from './index.js'; ^^^^^^ SyntaxError: Named export 'verify' not found. The requested module './index.js' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: ``` With this change: Successful execution (in this case, a "jwt malformed" error). This issue is further documented as the ["Named Exports"](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NamedExports.md) error on [Are the Types Wrong](https://arethetypeswrong.github.io/?p=jsonwebtoken%409.0.2). Fixes #963, #875 --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 161eb2dd..e7f5e747 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,15 @@ +const decode = require('./decode'); +const verify = require('./verify'); +const sign = require('./sign'); +const JsonWebTokenError = require('./lib/JsonWebTokenError'); +const NotBeforeError = require('./lib/NotBeforeError'); +const TokenExpiredError = require('./lib/TokenExpiredError'); + module.exports = { - decode: require('./decode'), - verify: require('./verify'), - sign: require('./sign'), - JsonWebTokenError: require('./lib/JsonWebTokenError'), - NotBeforeError: require('./lib/NotBeforeError'), - TokenExpiredError: require('./lib/TokenExpiredError'), + decode, + verify, + sign, + JsonWebTokenError, + NotBeforeError, + TokenExpiredError, };