question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vite unable to use npm xml-js package: Cannot read properties of undefined (reading 'prototype') at sax.js:222:46 at node_modules/sax/lib/sax.js

See original GitHub issue

Describe the bug

Current web app project using Rollup and am able to use the xml-js package to build the web app, however, when trying to switch Vite, the web app fails to load and Vite produces no errors, however, in the console I get the error below at runtime. I reproduced this using the simplest Vite+Vue app on stackblitz (see link below).

image

Text:

Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
    at sax.js:222:46
    at node_modules/sax/lib/sax.js (sax.js:1565:1)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at node_modules/xml-js/lib/xml2js.js (xml2js.js:1:11)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at node_modules/xml-js/lib/index.js (index.js:3:14)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at dep:xml-js:1:16
| (anonymous) | @ | sax.js:222
| node_modules/sax/lib/sax.js | @ | sax.js:1565
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| node_modules/xml-js/lib/xml2js.js | @ | xml2js.js:1
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| node_modules/xml-js/lib/index.js | @ | index.js:3
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| (anonymous) | @ | dep:xml-js:1

xml-js: https://www.npmjs.com/package/xml-js It appears that it fails to package Stream, but Rollup can package it, and I understand Vite uses Rollup internally, so I’m confused as to why this is failing.

When reproducing using the stackblitz web IDE, please open console to see the error. image

Reproduction

https://stackblitz.com/edit/vitejs-vite-fm1ze8

System Info

System:    OS: Windows 10 10.0.18363
  Binaries:    Node: 14.16.0 - C:\Program Files\nodejs\node-v14.16.0\node.EXE
  vite: ^2.9.0

Used Package Manager

npm

Logs

Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
    at sax.js:222:46
    at node_modules/sax/lib/sax.js (sax.js:1565:1)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at node_modules/xml-js/lib/xml2js.js (xml2js.js:1:11)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at node_modules/xml-js/lib/index.js (index.js:3:14)
    at __require (chunk-OL3AADLO.js?v=8029f249:9:50)
    at dep:xml-js:1:16
| (anonymous) | @ | sax.js:222
| node_modules/sax/lib/sax.js | @ | sax.js:1565
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| node_modules/xml-js/lib/xml2js.js | @ | xml2js.js:1
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| node_modules/xml-js/lib/index.js | @ | index.js:3
| __require | @ | chunk-OL3AADLO.js?v=8029f249:9
| (anonymous) | @ | dep:xml-js:1

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mattias-nuanxedcommented, Apr 5, 2022

This solution worked for me.

1reaction
bluwycommented, Apr 14, 2022

Maybe it is safe to change export default to module.exports =? (not sure if it works) Since require('externalized').foo will throw error when externalized was not resolved.

I think https://github.com/vitejs/vite/pull/6493/files#diff-7657c7c3217417a1747348410996718b471f6f0cb9552752b47a411f8f010830R207-R224 may address this, though I haven’t took the time to look into it yet. The PR is mixed with a fix for yarn pnp.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - TypeError: Cannot read properties of undefined ...
I've tried adding target: "node" to my "node_modules/react-scripts/config/webpack.config.js" But it won't work... Here's some ways I've already ...
Read more >
@vitejs/plugin-react - npm
The all-in-one Vite plugin for React projects. enable Fast Refresh in development; use the automatic JSX runtime; avoid manual import React in ....
Read more >
vite internal server error | The AI Search Engine You Control
So i figured it out, well, my boss of design did, all i had to do was delete the node_modules directory and run...
Read more >
Getting Started - Vite
However, some templates require a higher Node.js version to work, please upgrade if ... npm 6.x npm create vite@latest my-vue-app --template vue #...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found