Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: conflict with another middleware plugin #87

Open
railty opened this issue Apr 2, 2024 · 4 comments
Open

Bug: conflict with another middleware plugin #87

railty opened this issue Apr 2, 2024 · 4 comments

Comments

@railty
Copy link

railty commented Apr 2, 2024

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,
  },
})
  1. 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
Copy link

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
Copy link

jlarmstrongiv commented Apr 17, 2024

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
Copy link

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.

@FlooferLand
Copy link

In some cases using the "exclude" and "overrides" sections can sorta fix this issue.
Adding path: "path-browserify" to overrides solved it for me, server-side on Vercel at least. Using that on my local machine breaks with that missing module error, the only way to fix it is to remove the polyfill plugin as far as I'm aware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants