jrnjy
jrnjy

Reputation: 39

__vite-browser-external:3 Uncaught Error: Module "" has been externalized for browser compatibility. Cannot access ".readFileSync" in client code

Can anyone solve the error:

__vite-browser-external:3 Uncaught Error: Module "" has been externalized for browser compatibility. Cannot access ".readFileSync" in client code.

For the following code:

export async function loadXliff(src: string): Promise<XliffFile> {
    const xliff = readFileSync(src);
    return xliff12ToJs(xliff.toString());
}

I tried FiLeReader API, it ain't working. I also tried importing fs, still not working, its showing fs is unidentified.

Tried importing all the related imports, still does not work

Upvotes: 0

Views: 395

Answers (1)

Osman
Osman

Reputation: 1011

it was a struggle, but I've finally found a working solution for my case. For me the error occurred when I tried to build the vite project. These changes solved it:

vite.config.ts:

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineConfig({
//...

plugins: [ 
  nodePolyfills({ 
    include: ['fs'], 
    overrides: { fs: 'memfs' } 
  }),
  // other plugins ...
],
//...
})

make sure you've installed vite-plugin-node-polyfills

credits to this answer here

Upvotes: 1

Related Questions