• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

The Coding Couple

Pair programming is a lifetime commitment.

  • Home
  • Categories
    • Arduino
    • JavaScript
    • Python
    • Raspberry Pi
  • About Us
    • Privacy Policy
  • Contact

Uncaught TypeError: util.inherits is not a function

May 26, 2022 by Ashley

util.inherits is not a function error message

I recently ran into an issue when I tried to import a reference to jsonwebtoken into a new Vue 3 project configured with Vite. I’ve observed others encountering the same error (although through different circumstances) and I figured I’d document what I did to resolve the problem for me in case it helps someone else out there.

data-stream.js:39 Uncaught TypeError: util.inherits is not a function
    at node_modules/jws/lib/data-stream.js (data-stream.js:39:6)
    at __require (chunk-PWNRNYY6.js?v=9f9eb6fa:23:50)
    at node_modules/jws/lib/sign-stream.js (sign-stream.js:3:18)
    at __require (chunk-PWNRNYY6.js?v=9f9eb6fa:23:50)
    at node_modules/jws/index.js (index.js:2:18)
    at __require (chunk-PWNRNYY6.js?v=9f9eb6fa:23:50)
    at node_modules/jsonwebtoken/decode.js (decode.js:1:11)
    at __require (chunk-PWNRNYY6.js?v=9f9eb6fa:23:50)
    at node_modules/jsonwebtoken/index.js (index.js:2:11)
    at __require (chunk-PWNRNYY6.js?v=9f9eb6fa:23:50)

I’ve attempted several things before finally finding a solution that resolved the error. My failed attempts included:

  • Altering the import statement as described in this GitHub Issue.
  • Ensuring I saved @types/jsonwebtoken and declared module 'jsonwebtoken' in a .d.ts file as suggested in this StackOverflow answer.

I came across a GitHub issue with the same error and what resolved the issue for one person was modifying their webpack config file. For this Vue 3 project, I was using Vite, but this issue helped shift my focus from the import of jsonwebtoken to how Vite was configured. That’s when I stumbled upon another GitHub issue related to the util module that contained this comment:

util is a node builtin module and isn’t/won’t get polyfilled by vite
you will need to add polyfills for node builtins

Niputi (GitHub user)

This newfound knowledge is what ultimately led me to the solution of my problem: I needed to configure Vite to polyfill node builtin modules. I did not know how to do this, but I found an excellent tutorial on Medium with details on how to accomplish it.

I only needed to polyfill the util module, so there was a lot of configuration code that I was was able to omit that was included in the the Medium tutorial.

First I needed to install two new dependencies:

  • npm install --save-dev @esbuild-plugins/node-globals-polyfill
  • npm install --save-dev @esbuild-plugins/node-modules-polyfill

With the new dependencies, I added a new alias for the util module and under plugins, I added references to NodeGlobalPolyfillPlugin and NodeModulesPolyfilPlugin.

import { fileURLToPath, URL } from 'url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      util: 'rollup-plugin-node-polyfills/polyfills/util'
    }
  },
  optimizeDeps: {
    esbuildOptions: {
      plugins: [
            NodeGlobalsPolyfillPlugin({
                process: true,
                buffer: true
            }),
            NodeModulesPolyfillPlugin()
      ]
    }
  }
})

With that modification to vite.config.js, I was able to get pass the import error and start using the jsonwebtoken module.

An Important Update

I celebrated prematurely. The changes above resolved my issues when I ran the dev server, however, when I switched to the production build of the application a new (but related) issue emerged.

Console error

Digging into the source, it looked like the buffer node module needed to be polyfilled as well. I revisited the Medium article, and in the comments, a few people also ran into a similar issue. I started going down the path of trying to figure out a way to properly polyfill the node module before I stopped and viewed the problem from a different lens.

The reason Vite (and now Webpack 5) do not polyfill node builtin modules out of the box is because the practice is discouraged. The node builtin modules do not exist in the browser.

I swapped the use of the jsonwebtoken library with jose. (I discovered the existence of jose from jwt.io. They have a page dedicated to libraries and under the JavaScript section, I stumbled upon jose.) It was a relatively easy swap and I no longer have a need to polyfill builtin node modules.

~ Ashley

LET’S BE FRIENDS!

  • Instagram: @thecodingcouple
  • Twitter: @thecodingcouple
  • GitHub: @thecodingcouple
  • Other Blog Posts

Related Posts

  • Error: [🍍]: “getActivePinia()” was called but there was no active PiniaError: [🍍]: “getActivePinia()” was called but there was no active Pinia
  • Vue.JS Error: This relative module was not foundVue.JS Error: This relative module was not found
  • 5 Ways to Improve Your Debugging Skills5 Ways to Improve Your Debugging Skills
  • AdaBox 005: Break for Pi | Adafruit Subscription BoxAdaBox 005: Break for Pi | Adafruit Subscription Box
  • Snow Globe Single DIV CSS Drawing with Animating Snowflakes | TutorialSnow Globe Single DIV CSS Drawing with Animating Snowflakes | Tutorial
  • Creation Crate Month 3: An Arduino Powered Distance DetectorCreation Crate Month 3: An Arduino Powered Distance Detector

Filed Under: Debugging, JavaScript Tagged With: debugging, node module, troubleshooting, util, vite, vue, vue 3

Previous Post: « Snow Globe Single DIV CSS Drawing with Animating Snowflakes | Tutorial
Next Post: Using WSL on Corporate VPN »

Primary Sidebar

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Recent Posts

  • Pokémon Color Picker | A web app built with HTML/CSS + JavaScript
  • Pokéball Single DIV CSS Drawing | Tutorial
  • Error: [🍍]: “getActivePinia()” was called but there was no active Pinia
  • Trijam #261 Game Jam Diary: One Wrong Move
  • Using WSL on Corporate VPN

Recent Comments

  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley Grenon on Creation Crate Month 2: An Arduino Powered Memory Game
  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • kelly on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley on Creation Crate Month 3: An Arduino Powered Distance Detector

Follow us on Instagram!

This error message is only visible to WordPress admins

Error: No feed found.

Please go to the Instagram Feed settings page to create a feed.

Categories

  • Arduino
  • Conferences
  • Debugging
  • Game Jams
  • HTML and CSS
  • JavaScript
  • Programming Languages
  • Python
  • Raspberry Pi
  • Today I Learned

Archives

  • May 2024
  • April 2024
  • March 2024
  • May 2022
  • December 2021
  • May 2021
  • March 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • June 2019
  • April 2019
  • September 2017
  • April 2017
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • April 2015
  • January 2015

Tags

adafruit arduino brackets c# code smell codestock coding standards conventions creation crate debugging developer devspace electronics es6 es2015 game development game jam gotcha hackathon hoisting html html5 javascript led naming conventions nintendo phaser pluralsight pokemon programmer python raspberry pi retro retropie scope self improvement single div single div drawing subscription box TIL today I learned troubleshooting vue vuejs windbg

Footer

About Us

We are the Coding Couple.  Two people who met in college and decided they wanted to pair program for the rest of their ...

Read More »

Most Recent Posts

Pokémon Color Picker | A web app built with HTML/CSS + JavaScript

Pokéball Single DIV CSS Drawing | Tutorial

Error: [🍍]: “getActivePinia()” was called but there was no active Pinia

Trijam #261 Game Jam Diary: One Wrong Move

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright Notice

© The Coding Couple, 2015 – 2023. Excerpts and links may be used, provided that full and clear credit is given to The Coding Couple with appropriate and specific direction to the original content.

Copyright © 2025 · Foodie Pro Theme by Shay Bocks · Built on the Genesis Framework · Powered by WordPress