davidmyersdev/vite-plugin-node-polyfills

Importing fs returns a null object.

iiSHZUKA opened this issue · 5 comments

For some reason, importing node:fs returns a null object, any other polyfill works..?

import path from 'node:path'; // {resolve: ƒ, normalize: ƒ, isAbsolute: ƒ, join: ƒ, relative: ƒ, …} 
import http from 'node:http'; // {request: ƒ, get: ƒ, ClientRequest: ƒ, IncomingMessage: ƒ, Agent: ƒ, …}
import fs from 'node:fs'; // null

So, what could've gone wrong?
I'm not excluding the fs by the way~, Here is my vite.config.js:

// vite.config.js
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineConfig({
    plugins: [
        nodePolyfills({
            globals: {
                Buffer: true, 
                global: true, 
                process: true, 
            },
            protocolImports: true,
        }),
    ]
});

Hey there, @iiSHZUKA. 👋 I really appreciate the example config. Can you please provide a link to a reproduction on CodeSandbox or StackBlitz?

Hey there, @iiSHZUKA. 👋 I really appreciate the example config. Can you please provide a link to a reproduction on CodeSandbox or StackBlitz?

I doubt that the project setup itself is the problem, as I tried to test it on a completely new plain project here and it still did not work. Maybe I am missing something, but there is nothing more to do from your README.md~

@iiSHZUKA thanks so much for that link. It's less about the project setup and more about the time-savings for me to be able to jump right into the problem when I have time to work on this project. Since fs could be polyfilled a number of different ways, I think it makes more sense to allow overrides to be specified based on your own use case. The latest version (v0.14.0) allows you to do this with a new overrides config option. Can you try it out by installing an fs-specific polyfill (e.g. memfs)? The config would look something like this:

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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    nodePolyfills({
      overrides: {
        fs: 'memfs',
      },
    }),
  ],
})

@davidmyersdev Thank you for the update. I've verified that it works as expected, and solves the issue.
I appreciate your quick fix and work.

@iiSHZUKA That's great to hear! Thanks so much for following up!