javascriptnode.jsvitenode.js-fs

Am I overlooking the solution to "Module 'fs' has been externalized for browser compatibility"?


Just so you get an understanding of the situation, I am trying to build a web game library that runs on my computer. It is pretty much a rip off of the Nintendo Wii and 3DS. I'm using Vite as the server I use to access the webpages, and WS so I can run multiplayer games and services. The next thing I want to do is file manipulation so I can create save files for whatever game, but I have been having issues with file manipulation.

I have a test run file that is suppose to change the text of "hello.txt" from "Hello there" to "Good Bye". I am using Node/fs to edit files, and when I import fs after I run index.js, I get this error: Uncaught Error: Module "fs" has been externalized for browser compatibility. Cannot access "fs.fs" in client code.

I looked all over the internet to find the solution to this problem, and I have found it, but I still get this error.

For clarification, the answer was to install vite-plugin-node-polyfills and type this into vite.config.ts:

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

export default defineConfig({
  plugins: [
    nodePolyfills(),
  ],
})

Is there more to this solution that I am missing, or am I doing it all wrong? Also what does, "Cannot access 'fs.fs' in client code," mean? Is this something I'm completely overlooking?

If you need anymore information, I'll be here.


Solution

  • Now that I have more understanding and context on programming, Module "fs" has been externalized for browser compatibility means that fs is NOT compatible client-side (the users browser).