Vite build fails, 'emitter' is imported by emitter?commonjs-external, but could not be resolved – treating it as an external dependency

Viewed 21

First of all link to the issue

https://stackblitz.com/edit/vue3-script-setup-with-vite-mx4xx2?file=package.json

To reproduce the problem run command

npm run build

Then you get error in logs

transforming (119) node_modules/lodash/_hashDelete.js'emitter' is imported by emitter?commonjs-external, but could not be resolved – treating it as an external dependency

Trying to run npm run serve gives console error

Uncaught TypeError: Failed to resolve module specifier "emitter". Relative references must start with either "/", "./", or "../".

I know this error is caused by package geostyler-sld-parser, specificaly this line in App.vue

const sldParser = new SLDParser()

I need this package and run build/serve successfully, anyone knows how to configure vite correctly? I tried some plugins but nothing worked so far.

1 Answers

Okay, so the issue was with dependency stream v0.0.2 that geostyler-sld-parser used.

To fix it I added alias for emitter so stream package resolves emitter correctly.

resolve: {
    alias: [
      {
        emitter: require.resolve('emitter-component'),
      },
    ]

Another solution that I considered is using patch-package module and fix dependency by hand in node modules.

Related