Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #7555

Closed
7 tasks done
rrabb opened this issue Apr 1, 2022 · 8 comments · Fixed by #8338
Labels
inconsistency Inconsistency between dev & build p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@rrabb
Copy link

rrabb commented Apr 1, 2022

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

@mattias-nuanxed

This comment was marked as spam.

@mdxprograms

This comment was marked as spam.

@mattias-nuanxed
Copy link

mattias-nuanxed commented Apr 5, 2022

This solution worked for me.

@sapphi-red sapphi-red added the inconsistency Inconsistency between dev & build label Apr 13, 2022
@sapphi-red
Copy link
Member

sapphi-red commented Apr 13, 2022

It works with build but it does not work during dev.

This part becomes like this after optimization.
https://github.com/isaacs/sax-js/blob/5da00d213a8cae94be72ce155e7cf8fda600b94c/lib/sax.js#L161-L166

      var Stream;
      try {
        Stream = (init_stream(), __toCommonJS(stream_exports)).Stream;
      } catch (ex) {
        Stream = function() {
        };
      }
// browser-external:stream
var stream_exports = {};
__export(stream_exports, {
  default: () => stream_default
});
var stream_default;
var init_stream = __esm({
  "browser-external:stream"() {
    stream_default = new Proxy({}, {
      get() {
        throw new Error('Module "stream" has been externalized for browser compatibility and cannot be accessed in client code.');
      }
    });
  }
});

If Stream = (init_stream(), __toCommonJS(stream_exports)).Stream; throws an error, maybe it works.
But currently it becomes undefined.

@sapphi-red
Copy link
Member

I don't know why this only throws error with .default.

build.onLoad(
{ filter: /.*/, namespace: 'browser-external' },
({ path: id }) => {
return {
contents:
`export default new Proxy({}, {
get() {
throw new Error('Module "${id}" has been externalized for ` +
`browser compatibility and cannot be accessed in client code.')
}
})`
}
}
)

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.

@sapphi-red
Copy link
Member

For a workaround, aliasing stream to a polyfill (for example stream-browserify) works.

// vite.config.js
export default defineConfig({
  resolve: {
    alias: {
      'stream': 'stream-browserify'
    }
  }
});

@sapphi-red sapphi-red added p2-edge-case Bug, but has workaround or limited in scope (priority) bug and removed pending triage labels Apr 13, 2022
@bluwy
Copy link
Member

bluwy commented 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.

@livthomas
Copy link
Contributor

I have also run into this problem when I added @foxglove/xmlrpc dependency to my project. Here's how I solved it:

  1. I added the following lines to main.ts file:
    import {Buffer} from 'buffer';
    import EventEmitter from 'events';
    import process from 'process';
    
    // sax.js needs these
    window.Buffer = Buffer;
    window.EventEmitter = EventEmitter;
    window.process = process;
  2. And then I added the following lines to vite.config.ts file:
    {
      build: {
        commonjsOptions: {
          ignoreTryCatch: id => id !== 'stream',
        },
      },
      resolve: {
        alias: [
          {
            find: 'stream',
            replacement: `stream-browserify`,
          },
        ],
      },
    }

It works in both dev and prod mode.

@sapphi-red sapphi-red linked a pull request May 30, 2022 that will close this issue
9 tasks
@sapphi-red sapphi-red mentioned this issue Jun 18, 2022
7 tasks
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
inconsistency Inconsistency between dev & build p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants