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

Bug: conflict with another middleware plugin #87

Open railty opened 3 months ago

railty commented 3 months ago

Summary

very helpful plugin, but I seems run into a conflict with an express server plugin.

  1. brand new react app generate by vite (JavaScript + SWC), version "vite": "^5.2.0", "vite-plugin-node-polyfills": "^0.21.0"
  2. vite.config.js is
    
    import { defineConfig } from 'vite'
    import react from '@vitejs/plugin-react-swc'
    import { nodePolyfills } from 'vite-plugin-node-polyfills'

function express(path) { return { name: "vite-plugin-express", configureServer: async (server) => { server.middlewares.use(async (req, res, next) => { process.env["VITE"] = "true"; try { const { app } = await server.ssrLoadModule(path); app(req, res, next); } catch (err) { console.error(err); } }); }, }; }

export default defineConfig({ plugins: [nodePolyfills(), react(), express('src/server')], server: { port: 3000, }, })

3. the src/server.js is 

import express from 'express';

//the following 3 lines, (actualy the first line import) will hang the server / import path from 'path'; const rootPath = path.dirname(new URL(import.meta.url).pathname); console.log(rootPath); /

export const app = express(); app.use(express.json());

app.get('/api/hello', (req, res) => { res.status(200).json({
message: 'hello', }); });

now, with the 3 lines (import path) commented, everything works. or if I un-comment these 3 lines but remove the nodePolyfills() plugin, everything still works. 
But If I have the nodePolyfills() plugin, and have import path in the server file, server will hang. the error messages are

12:33:07 AM [vite] Error when evaluating SSR module /node_modules/path-browserify/index.js: |- ReferenceError: module is not defined at eval (node_modules/path-browserify/index.js:531:1) at instantiateModule (file:///node_modules/vite/dist/node/chunks/dep-C-KAszbv.js:55006:15)

12:33:07 AM [vite] Error when evaluating SSR module src/server: failed to import "/node_modules/path-browserify/index.js" |- ReferenceError: module is not defined at eval node_modules/path-browserify/index.js:531:1) at instantiateModule (file:///node_modules/vite/dist/node/chunks/dep-C-KAszbv.js:55006:15)

jlarmstrongiv commented 2 months ago

I have a similar error, but from Astro middleware:

2:42:08 PM [vite] Error when evaluating SSR module /@fs/Users/user/Desktop/projects/project/node_modules/path-browserify/index.js:
|- ReferenceError: module is not defined
    at eval (/Users/user/Desktop/projects/project/node_modules/path-browserify/index.js:531:1)
    at instantiateModule (file:///Users/user/Desktop/projects/project/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:55089:15)
jlarmstrongiv commented 2 months ago

However, the global shims, etc. still work.

I think this error comes from any polyfills that do not support ESModules. If I patch path-browserify to use export default = instead of module.exports =, then the error goes away for the middleware.

jlarmstrongiv commented 2 months ago

Finding alternative polyfills like pathe for path seems to resolve the issue. It would be good to check other polyfills, as some like process also fail.