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

Bug: Cannot destructure property 'existsSync' of 'require_empty(...) #95

Open
vitorioMarcapo opened this issue Jun 6, 2024 · 4 comments

Comments

@vitorioMarcapo
Copy link

vitorioMarcapo commented Jun 6, 2024

Summary

I am getting the following error when I added the plugin
sanitize-html.js?v=a55f9d5f:6974 Uncaught TypeError: Cannot destructure property 'existsSync' of 'require_empty(...)' as it is null.

It's coming from the postcss, but from my understanding postcss is a dependency of vite and I am not sure how to deal with this.

My config so far is:
Screenshot at Jun 06 12-10-32
:

The problem is with the fs and path modules. Here is visible they are used by postcss inside previous.map.js:
Screenshot at Jun 06 12-12-54

@vitorioMarcapo vitorioMarcapo changed the title Bug: Bug: Cannot destructure property 'existsSync' of 'require_empty(...) Jun 6, 2024
@Gitssalah
Copy link

Hello any updates ?

@luisamiranda
Copy link

I am having the same issue. Has anyone found a solution?

@Gitssalah
Copy link

Gitssalah commented Aug 5, 2024

The issue arises because polyfills substitute Node.js's fs module with a mock fs that is compatible with the browser but lacks certain methods like existsSync. I faced a similar problem, but since I was working with Electron.js, I resolved it by creating a new fs instance using contextBridge.e

//preload.js
contextBridge.exposeInMainWorld('electron', {
//...
fs: {
    statSync: (filePath) => fs.statSync(filePath),
    isDirectory: (filePath) => {
      const stats = fs.statSync(filePath);
      return stats.isDirectory();
    },
    isFile: (filePath) => {
      const stats = fs.statSync(filePath);
      return stats.isFile();
    },
    lstatSync: (...args) => { return fs.lstatSync(...args) },
    writeFileSync: (...args) => { return fs.writeFileSync(...args) },
    readFileSync: (path, ...args) => { return fs.readFileSync(path, ...args) }
    //in your case add existSync

  },

If you’re not using Electron, which appears to be the case, you can try adding the following to your vite.config.js:

export default defineConfig({
  plugins: [
    nodePolyfills({
      overrides: {
        // Since `fs` is not supported in browsers, we can use mock fs packages to polyfill it.
        fs: 'browserify-fs',
      },
    }),
//...

@luisamiranda
Copy link

The override was what I needed. Thank you.

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

3 participants