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

Uncaught ReferenceError: require is not defined in ViteJS #240

Open
Kukunin opened this issue Feb 23, 2022 · 3 comments
Open

Uncaught ReferenceError: require is not defined in ViteJS #240

Kukunin opened this issue Feb 23, 2022 · 3 comments

Comments

@Kukunin
Copy link

Kukunin commented Feb 23, 2022

Describe the bug
The package includes require that causes an exception in a browser bundle built by ViteJS. It happens because of these lines:

https://github.com/squirrellyjs/squirrelly/blob/master/src/file-utils.ts#L1-L2

That file should be imported only in nodejs build, not a browser. At least, it should not be included in ESM format

@dahei
Copy link

dahei commented Mar 27, 2023

I was able to make it work via passing transformMixedEsModules: true to the commonJsOptions:

export default defineConfig({
  ...
  build: {
    commonjsOptions: {
      transformMixedEsModules: true
    },
  }
})

Background: https://stackoverflow.com/a/63099935/1574484

@jnsprnw
Copy link

jnsprnw commented Apr 20, 2023

To make this solution more visible for people with the same error, I want to add that this is happening with Sveltekit and the Netlify-adapter. @dahei’s solution added to vite.config.js worked for me.

@lisilinhart
Copy link

lisilinhart commented Nov 17, 2023

Another solution is to import the browser minified code instead of the regular export of the package when you're running this package in the browser :

import * as Sqrl from 'squirrelly/dist/browser/squirrelly.min.js'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants