I get Uncaught TypeError: Buffer2 is undefined when I import jsonwebtoken into my react project.
Both import jwt from jsonwebtoken and import {decode} from 'jsonwebtoken' raise the error.
I am using jsonwebtoken v8.5.1. I am using vite v2.7.2 as my build tool.
What could be causing the issue?
Stack Trace
Uncaught TypeError: Buffer2 is undefined
js index.js:11
__require chunk-ESTXZSVW.js:12
js sign-stream.js:2
__require chunk-ESTXZSVW.js:12
js index.js:2
__require chunk-ESTXZSVW.js:12
js decode.js:1
__require chunk-ESTXZSVW.js:12
js index.js:2
__require chunk-ESTXZSVW.js:12
<anonymous> jsonwebtoken:1
index.js:11:4
js index.js:11
__require chunk-ESTXZSVW.js:12
js sign-stream.js:2
__require chunk-ESTXZSVW.js:12
js index.js:2
__require chunk-ESTXZSVW.js:12
js decode.js:1
__require chunk-ESTXZSVW.js:12
js index.js:2
__require chunk-ESTXZSVW.js:12
<anonymous> jsonwebtoken:1
InnerModuleEvaluation self-hosted:2374
InnerModuleEvaluation self-hosted:2374
InnerModuleEvaluation self-hosted:2374
evaluation self-hosted:2335
The problem was with Webpack dropping support for polyfills. The only reason I found out about it is because I recreated the project in create-react-app and it showed me a really helpful error message. Vite just showed me a blank page.
More Details: https://webpack.js.org/blog/2020-10-10-webpack-5-release/#automatic-nodejs-polyfills-removed