davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

Nuxt3 - Cannot read properties of null & fs #51

Closed trumanjchan closed 8 months ago

trumanjchan commented 8 months ago

Hi! I read I should post this in Discussions but I couldn't find the tab, sorry!

I have this error, so I did bun install -D vite-plugin-node-polyfill and thought that would import fs correctly. I'm still getting this issue where my Nuxtjs3 web app, upon refreshing the page, shows a brief flash of everything being loaded then showing the 500: Cannot read properties of null (reading '__esModule') error pointing at the import fs from 'fs' line.

I'm not sure if I imported the plugin correctly into my nuxt.config.ts file. I also currently don't have a /plugins directory. I'm using node v18.0.0 and bun as my package manager.

Any help or ideas is much appreciated! Thank you!

Screenshot 2023-10-20 at 5 37 15 PM Screenshot 2023-10-20 at 5 38 02 PM Screenshot 2023-10-20 at 5 38 24 PM

Currently with import fs from 'fs':

Screenshot 2023-10-20 at 5 44 10 PM

If I change the import line to import * as fs from 'fs':

Screenshot 2023-10-20 at 5 45 17 PM

If I change the import line to const fs = require('fs'):

Screenshot 2023-10-20 at 5 43 30 PM
davidmyersdev commented 8 months ago

Hello, @trumanjchan. This error is expected because the implementation for fs is null by default. You can use a package such as memfs (or something else depending on your needs) along with the overrides config option to polyfill fs. See https://github.com/davidmyersdev/vite-plugin-node-polyfills/issues/43 for more info (this is a duplicate of that issue).